1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
10 |
|
|
**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** USBX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** Device Storage Class */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
#define UX_SOURCE_CODE |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
/* Include necessary system files. */ |
27 |
|
|
|
28 |
|
|
#include "ux_api.h" |
29 |
|
|
#include "ux_device_class_storage.h" |
30 |
|
|
#include "ux_device_stack.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
#if UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE < UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH |
34 |
|
|
/* #error UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE too small, please check */ |
35 |
|
|
/* Build option checked runtime by UX_ASSERT */ |
36 |
|
|
#endif |
37 |
|
|
|
38 |
|
|
/**************************************************************************/ |
39 |
|
|
/* */ |
40 |
|
|
/* FUNCTION RELEASE */ |
41 |
|
|
/* */ |
42 |
|
|
/* _ux_device_class_storage_read_format_capacity PORTABLE C */ |
43 |
|
|
/* 6.3.0 */ |
44 |
|
|
/* AUTHOR */ |
45 |
|
|
/* */ |
46 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
47 |
|
|
/* */ |
48 |
|
|
/* DESCRIPTION */ |
49 |
|
|
/* */ |
50 |
|
|
/* This function performs a READ_FORMAT_CAPACITY command. */ |
51 |
|
|
/* */ |
52 |
|
|
/* INPUT */ |
53 |
|
|
/* */ |
54 |
|
|
/* storage Pointer to storage class */ |
55 |
|
|
/* lun Logical unit number */ |
56 |
|
|
/* endpoint_in Pointer to IN endpoint */ |
57 |
|
|
/* endpoint_out Pointer to OUT endpoint */ |
58 |
|
|
/* cbwcb Pointer to CBWCB */ |
59 |
|
|
/* */ |
60 |
|
|
/* OUTPUT */ |
61 |
|
|
/* */ |
62 |
|
|
/* Completion Status */ |
63 |
|
|
/* */ |
64 |
|
|
/* CALLS */ |
65 |
|
|
/* */ |
66 |
|
|
/* _ux_device_stack_transfer_request Transfer request */ |
67 |
|
|
/* _ux_utility_memory_set Set memory */ |
68 |
|
|
/* _ux_utility_long_put_big_endian Put 32-bit big endian */ |
69 |
|
|
/* */ |
70 |
|
|
/* CALLED BY */ |
71 |
|
|
/* */ |
72 |
|
|
/* Device Storage Class */ |
73 |
|
|
/* */ |
74 |
|
|
/**************************************************************************/ |
75 |
|
355 |
UINT _ux_device_class_storage_read_format_capacity(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, |
76 |
|
|
UX_SLAVE_ENDPOINT *endpoint_in, |
77 |
|
|
UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb) |
78 |
|
|
{ |
79 |
|
|
|
80 |
|
|
UINT status; |
81 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
82 |
|
|
UCHAR *read_format_capacity_buffer; |
83 |
|
|
|
84 |
|
|
UX_PARAMETER_NOT_USED(cbwcb); |
85 |
|
|
UX_PARAMETER_NOT_USED(endpoint_out); |
86 |
|
|
|
87 |
|
|
/* Build option check. */ |
88 |
|
|
UX_ASSERT(UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE >= UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH); |
89 |
|
|
|
90 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
91 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_READ_FORMAT_CAPACITY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0) |
92 |
|
|
|
93 |
✗✓ |
355 |
if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_medium_loaded_status == 0) |
94 |
|
|
{ |
95 |
|
|
/* Media not loaded. Set NOT READY sense code. */ |
96 |
|
|
storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = |
97 |
|
|
UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(UX_SLAVE_CLASS_STORAGE_SENSE_KEY_NOT_READY, |
98 |
|
|
UX_SLAVE_CLASS_STORAGE_SENSE_CODE_NOT_PRESENT, 0x00); |
99 |
|
|
|
100 |
|
|
/* Return CSW with failure. */ |
101 |
|
|
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED; |
102 |
|
|
|
103 |
|
|
/* Return completion status. */ |
104 |
|
|
return(UX_SUCCESS); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/* Obtain the pointer to the transfer request. */ |
108 |
|
355 |
transfer_request = &endpoint_in -> ux_slave_endpoint_transfer_request; |
109 |
|
|
|
110 |
|
|
/* Get read format capacity response buffer. */ |
111 |
|
355 |
read_format_capacity_buffer = transfer_request -> ux_slave_transfer_request_data_pointer; |
112 |
|
|
|
113 |
|
|
/* Ensure it is cleaned. */ |
114 |
|
355 |
_ux_utility_memory_set(read_format_capacity_buffer, 0, UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH); /* Use case of memset is verified. */ |
115 |
|
|
|
116 |
|
|
/* Insert the size of the response block. */ |
117 |
|
355 |
_ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_SIZE], 8); |
118 |
|
|
|
119 |
|
|
/* Insert the last LBA address in the response. */ |
120 |
|
355 |
_ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LAST_LBA], |
121 |
|
|
storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_last_lba); |
122 |
|
|
|
123 |
|
|
/* Insert the block length in the response. This is in 3 bytes. */ |
124 |
|
355 |
_ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_BLOCK_SIZE], |
125 |
|
|
storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length); |
126 |
|
|
|
127 |
|
|
/* Insert the response code : always 2. */ |
128 |
|
355 |
read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_DESC_CODE] = 2; |
129 |
|
|
|
130 |
|
|
#if defined(UX_DEVICE_STANDALONE) |
131 |
|
|
|
132 |
|
|
/* Next: Transfer (DATA). */ |
133 |
|
|
storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START; |
134 |
|
|
storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ; |
135 |
|
|
|
136 |
|
|
storage -> ux_device_class_storage_transfer = transfer_request; |
137 |
|
|
storage -> ux_device_class_storage_device_length = |
138 |
|
|
UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH; |
139 |
|
|
storage -> ux_device_class_storage_data_length = |
140 |
|
|
UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH; |
141 |
|
|
storage -> ux_device_class_storage_data_count = 0; |
142 |
|
|
UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer); |
143 |
|
|
|
144 |
|
|
#else |
145 |
|
|
|
146 |
|
|
/* Send a data payload with the read_capacity response buffer. */ |
147 |
|
355 |
_ux_device_stack_transfer_request(transfer_request, |
148 |
|
|
UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH, |
149 |
|
|
UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH); |
150 |
|
|
#endif |
151 |
|
|
|
152 |
|
|
/* Now we set the CSW with success. */ |
153 |
|
355 |
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED; |
154 |
|
355 |
status = UX_SUCCESS; |
155 |
|
|
|
156 |
|
|
/* Return completion status. */ |
157 |
|
355 |
return(status); |
158 |
|
|
} |