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 |
|
|
/** Host Stack */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
/* Include necessary system files. */ |
25 |
|
|
|
26 |
|
|
#define UX_SOURCE_CODE |
27 |
|
|
|
28 |
|
|
#include "ux_api.h" |
29 |
|
|
#include "ux_host_stack.h" |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
/**************************************************************************/ |
33 |
|
|
/* */ |
34 |
|
|
/* FUNCTION RELEASE */ |
35 |
|
|
/* */ |
36 |
|
|
/* _ux_host_stack_device_address_set PORTABLE C */ |
37 |
|
|
/* 6.1.10 */ |
38 |
|
|
/* AUTHOR */ |
39 |
|
|
/* */ |
40 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
41 |
|
|
/* */ |
42 |
|
|
/* DESCRIPTION */ |
43 |
|
|
/* */ |
44 |
|
|
/* This function sets the device address to the new device. */ |
45 |
|
|
/* */ |
46 |
|
|
/* INPUT */ |
47 |
|
|
/* */ |
48 |
|
|
/* device Pointer to device */ |
49 |
|
|
/* */ |
50 |
|
|
/* OUTPUT */ |
51 |
|
|
/* */ |
52 |
|
|
/* Completion Status */ |
53 |
|
|
/* */ |
54 |
|
|
/* CALLS */ |
55 |
|
|
/* */ |
56 |
|
|
/* _ux_utility_delay_ms Thread sleep */ |
57 |
|
|
/* _ux_host_stack_transfer_request Process transfer request */ |
58 |
|
|
/* */ |
59 |
|
|
/* CALLED BY */ |
60 |
|
|
/* */ |
61 |
|
|
/* USBX Components */ |
62 |
|
|
/* */ |
63 |
|
|
/**************************************************************************/ |
64 |
|
1505 |
UINT _ux_host_stack_device_address_set(UX_DEVICE *device) |
65 |
|
|
{ |
66 |
|
|
|
67 |
|
|
|
68 |
|
1505 |
UINT status = UX_ERROR; |
69 |
|
|
UX_TRANSFER *transfer_request; |
70 |
|
|
UX_ENDPOINT *control_endpoint; |
71 |
|
|
USHORT device_address; |
72 |
|
|
#if UX_MAX_DEVICES > 1 |
73 |
|
|
UX_HCD *hcd; |
74 |
|
|
UINT address_byte_index; |
75 |
|
|
UINT address_bit_index; |
76 |
|
|
UCHAR device_address_byte; |
77 |
|
|
#endif |
78 |
|
|
|
79 |
|
|
/* Retrieve the pointer to the control endpoint. */ |
80 |
|
1505 |
control_endpoint = &device -> ux_device_control_endpoint; |
81 |
|
|
|
82 |
|
|
/* Retrieve the transfer request pointer. */ |
83 |
|
1505 |
transfer_request = &control_endpoint -> ux_endpoint_transfer_request; |
84 |
|
|
|
85 |
|
|
/* Initialize device address to 1. */ |
86 |
|
1505 |
device_address = 1; |
87 |
|
|
|
88 |
|
|
#if UX_MAX_DEVICES > 1 |
89 |
|
|
|
90 |
|
|
/* We need the HCD pointer as well. */ |
91 |
|
1505 |
hcd = UX_DEVICE_HCD_GET(device); |
92 |
|
|
|
93 |
|
|
/* Calculate the new address of this device. We start with address 1. */ |
94 |
✓✓ |
1522 |
for (address_byte_index = 0; address_byte_index < 16; address_byte_index++) |
95 |
|
|
{ |
96 |
|
|
|
97 |
|
|
/* Get the address mask byte. */ |
98 |
|
1521 |
device_address_byte = hcd -> ux_hcd_address[address_byte_index]; |
99 |
|
|
|
100 |
|
|
/* Scan each bit for an empty spot. */ |
101 |
✓✓ |
1671 |
for (address_bit_index = 0; address_bit_index < 8; address_bit_index++) |
102 |
|
|
{ |
103 |
|
|
|
104 |
✓✓ |
1654 |
if ((device_address_byte & (1 << address_bit_index)) == 0) |
105 |
|
|
{ |
106 |
|
|
|
107 |
|
|
/* We have found an empty spot. Reserve this address. */ |
108 |
|
1504 |
device_address_byte = (UCHAR)((UCHAR)device_address_byte | (UCHAR)(1 << address_bit_index)); |
109 |
|
|
|
110 |
|
|
/* Store the address mask byte. */ |
111 |
|
1504 |
hcd -> ux_hcd_address[address_byte_index] = device_address_byte; |
112 |
|
|
|
113 |
|
|
/* OK, apply address. */ |
114 |
|
1504 |
status = UX_SUCCESS; |
115 |
|
1504 |
break; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/* This address was already taken, increment to the next address. */ |
119 |
|
150 |
device_address++; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/* If address found, break the loop. */ |
123 |
✓✓ |
1521 |
if (status == UX_SUCCESS) |
124 |
|
|
{ |
125 |
|
1504 |
break; |
126 |
|
|
} |
127 |
|
|
} |
128 |
✓✓ |
1505 |
if (status == UX_ERROR) |
129 |
|
|
|
130 |
|
|
/* We should never get here! */ |
131 |
|
1 |
return(UX_ERROR); |
132 |
|
|
#endif |
133 |
|
|
|
134 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
135 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_ADDRESS_SET, device, device_address, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0) |
136 |
|
|
|
137 |
|
|
/* Create a transfer request for the SET_ADDRESS request. */ |
138 |
|
1504 |
transfer_request -> ux_transfer_request_data_pointer = UX_NULL; |
139 |
|
1504 |
transfer_request -> ux_transfer_request_requested_length = 0; |
140 |
|
1504 |
transfer_request -> ux_transfer_request_function = UX_SET_ADDRESS; |
141 |
|
1504 |
transfer_request -> ux_transfer_request_type = UX_REQUEST_OUT | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE; |
142 |
|
1504 |
transfer_request -> ux_transfer_request_value = device_address; |
143 |
|
1504 |
transfer_request -> ux_transfer_request_index = 0; |
144 |
|
|
|
145 |
|
|
#if defined(UX_HOST_STANDALONE) |
146 |
|
|
device -> ux_device_enum_trans = transfer_request; |
147 |
|
|
status = UX_SUCCESS; |
148 |
|
|
return(status); |
149 |
|
|
#else |
150 |
|
|
|
151 |
|
|
/* Send request to HCD layer. */ |
152 |
|
1504 |
status = _ux_host_stack_transfer_request(transfer_request); |
153 |
|
|
|
154 |
|
|
/* Now, this address will be the one used in future transfers. The transfer may have failed and therefore |
155 |
|
|
all the device resources including the new address will be free.*/ |
156 |
|
1504 |
device -> ux_device_address = (ULONG) device_address; |
157 |
|
|
|
158 |
|
|
/* Check completion status. */ |
159 |
✓✓ |
1504 |
if (status == UX_SUCCESS) |
160 |
|
|
{ |
161 |
|
|
|
162 |
|
|
/* Mark the device as ADDRESSED now. */ |
163 |
|
1087 |
device -> ux_device_state = UX_DEVICE_ADDRESSED; |
164 |
|
|
|
165 |
|
|
/* Some devices need some time to accept this address. */ |
166 |
|
1087 |
_ux_utility_delay_ms(UX_DEVICE_ADDRESS_SET_WAIT); |
167 |
|
|
|
168 |
|
|
/* Return successful status. */ |
169 |
|
1087 |
return(status); |
170 |
|
|
} |
171 |
|
|
else |
172 |
|
|
{ |
173 |
|
|
|
174 |
|
|
/* We have an error at the first device transaction. This is mostly |
175 |
|
|
due to the device having failed on the reset after power up. |
176 |
|
|
we will try again either at the root hub or regular hub. */ |
177 |
|
417 |
return(status); |
178 |
|
|
} |
179 |
|
|
#endif |
180 |
|
|
} |