Skip to content

Commit 2a1d09b

Browse files
committed
dwc3: code clean-up
Signed-off-by: souravr <sourav.raj@ignitarium.com>
1 parent 0e7b3a1 commit 2a1d09b

3 files changed

Lines changed: 49 additions & 92 deletions

File tree

src/portable/socfpga/dwc3/hcd_dwc3.c

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
static void hcd_xhci_set_configuration();
4747
static void hcd_dwc3_update_device_address( uint8_t daddr );
4848

49-
Usb3_Handle_t *Usb3handle;
49+
struct xhci_data xhci_handle __attribute__((aligned(64)));
5050
uint8_t device_addr = 0;
5151

5252
static struct xhci_int_desc usb3_int_desc;
@@ -73,13 +73,6 @@ bool hcd_dwc3_init( uint8_t rhport, const tusb_rhport_init_t *rh_init )
7373
return false;
7474
}
7575

76-
Usb3handle = alloc_usb_port();
77-
if (Usb3handle == NULL)
78-
{
79-
ERROR("Cannot allocate memory!!!");
80-
return false;
81-
}
82-
8376
ret = init_hcd_params();
8477
if (ret != 0)
8578
{
@@ -94,9 +87,12 @@ bool hcd_dwc3_init( uint8_t rhport, const tusb_rhport_init_t *rh_init )
9487
return false;
9588
}
9689

97-
if( xhci_init(&Usb3handle->xhci_priv) == false )
90+
//clear xHCI data structure members
91+
tu_memclr(&xhci_handle, sizeof(xhci_handle));
92+
93+
if( xhci_init(&xhci_handle) == false )
9894
{
99-
ERROR("xHCI init failed -%d!!!", ret);
95+
ERROR("xHCI init failed !!!");
10096
return false;
10197
}
10298
}
@@ -133,7 +129,6 @@ void hcd_dwc3_int_disable( uint8_t rhport )
133129
*--------------------------------------------------------------------+*/
134130
bool hcd_dwc3_port_connect_status( uint8_t rhport )
135131
{
136-
137132
bool ret;
138133

139134
ret = xhci_port_status(rhport);
@@ -143,16 +138,15 @@ bool hcd_dwc3_port_connect_status( uint8_t rhport )
143138

144139
void hcd_dwc3_port_reset( uint8_t rhport )
145140
{
146-
147141
(void) rhport;
148142
reset_usb_port(rhport);
149143
}
150144

151145
static bool hcd_enable_slot( void )
152146
{
153-
enable_slot_command(Usb3handle->xhci_priv.xcr_ring);
147+
enable_slot_command(xhci_handle.xcr_ring);
154148

155-
if (wait_for_command_completion_event(&Usb3handle->xhci_priv,
149+
if (wait_for_command_completion_event(&xhci_handle,
156150
ENABLE_SLOT_CMD) != 0)
157151
{
158152
return false;
@@ -165,43 +159,43 @@ static bool hcd_send_address_cmd( void )
165159
{
166160
int ret;
167161

168-
update_device_dev_speed(&Usb3handle->xhci_priv);
162+
update_device_dev_speed(&xhci_handle);
169163

170-
ret = init_input_device_context(&Usb3handle->xhci_priv);
164+
ret = init_input_device_context(&xhci_handle);
171165
if (ret != 0)
172166
{
173167
ERROR("XHCI Error -%d!!!", ret);
174168
return false;
175169
}
176170

177-
update_dcbaa_entry(&Usb3handle->xhci_priv);
171+
update_dcbaa_entry(&xhci_handle);
178172

179-
set_device_address(&Usb3handle->xhci_priv);
173+
set_device_address(&xhci_handle);
180174

181-
if (wait_for_command_completion_event(&Usb3handle->xhci_priv,
175+
if (wait_for_command_completion_event(&xhci_handle,
182176
ADDRESS_DEVICE_CMD) != 0)
183177
{
184178
return false;
185179
}
186180

187-
update_device_address(&Usb3handle->xhci_priv);
181+
update_device_address(&xhci_handle);
188182

189-
if (Usb3handle->xhci_priv.dev_data.dev_addr == 0U)
183+
if (xhci_handle.dev_data.dev_addr == 0U)
190184
{
191185
return false; /* device address can not be zero after address command is sent */
192186
}
193187

194-
display_xhci_device_params(&Usb3handle->xhci_priv.dev_data);
195-
display_ip_context(Usb3handle->xhci_priv.ip_ctx);
196-
display_op_context(Usb3handle->xhci_priv.op_ctx);
197-
display_event_trbs(&Usb3handle->xhci_priv);
188+
display_xhci_device_params(&xhci_handle.dev_data);
189+
display_ip_context(xhci_handle.ip_ctx);
190+
display_op_context(xhci_handle.op_ctx);
191+
display_event_trbs(&xhci_handle);
198192

199193
return true;
200194
}
201195

202196
static void hcd_dwc3_update_device_address( uint8_t daddr )
203197
{
204-
device_addr = 1U;
198+
device_addr = daddr;
205199
}
206200

207201
void hcd_dwc3_port_reset_end( uint8_t rhport )
@@ -254,20 +248,20 @@ void hcd_dwc3_device_close( uint8_t rhport )
254248
xhci_warm_reset(rhport);
255249
}
256250

257-
slotid = Usb3handle->xhci_priv.dev_data.slot_id;
251+
slotid = xhci_handle.dev_data.slot_id;
258252

259253
if (slotid != 0U)
260254
{
261-
disable_slot_command(Usb3handle->xhci_priv.xcr_ring, slotid);
255+
disable_slot_command(xhci_handle.xcr_ring, slotid);
262256

263-
if (wait_for_command_completion_event(&Usb3handle->xhci_priv,
257+
if (wait_for_command_completion_event(&xhci_handle,
264258
DISABLE_SLOT_CMD) != 0)
265259
{
266260
ERROR("xHCI command failed");
267261
return;
268262
}
269263

270-
dealloc_usb_port(Usb3handle);
264+
dealloc_usb_port(&xhci_handle);
271265
device_addr = 0U;
272266
usb_set_config = 0;
273267
}
@@ -278,15 +272,15 @@ void hcd_dwc3_device_close( uint8_t rhport )
278272
*--------------------------------------------------------------------+*/
279273
static void hcd_xhci_set_configuration()
280274
{
281-
update_xhc_slot_context(&Usb3handle->xhci_priv);
275+
update_xhc_slot_context(&xhci_handle);
282276

283-
init_xhc_endpoint_context(&Usb3handle->xhci_priv, BULK_OUT);
277+
init_xhc_endpoint_context(&xhci_handle, BULK_OUT);
284278

285-
init_xhc_endpoint_context(&Usb3handle->xhci_priv, BULK_IN);
279+
init_xhc_endpoint_context(&xhci_handle, BULK_IN);
286280

287-
configure_endpoint(&Usb3handle->xhci_priv);
281+
configure_endpoint(&xhci_handle);
288282

289-
if (wait_for_command_completion_event(&Usb3handle->xhci_priv,
283+
if (wait_for_command_completion_event(&xhci_handle,
290284
CONFIGURE_ENDPOINT_CMD) != 0)
291285
{
292286
return;
@@ -341,8 +335,8 @@ bool hcd_dwc3_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t
341335

342336
if( ep_num == 0 )
343337
{
344-
configure_setup_stage(&Usb3handle->xhci_priv, buffer, (usb_control_request_t *)&ctrl_req);
345-
ring_xhci_ep0_db(&Usb3handle->xhci_priv.op_regs);
338+
configure_setup_stage(&xhci_handle, buffer, (usb_control_request_t *)&ctrl_req);
339+
ring_xhci_ep0_db(&xhci_handle.op_regs);
346340
if( buffer != NULL )
347341
{
348342
usb_dcache_invalidate(buffer, buflen);
@@ -354,7 +348,7 @@ bool hcd_dwc3_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t
354348
{
355349
usb_dcache_clean(buffer, buflen);
356350
}
357-
endpoint_transfer(&Usb3handle->xhci_priv, (int) ep_num, (uint8_t) dir, buffer, buflen);
351+
endpoint_transfer(&xhci_handle, (int) ep_num, (uint8_t) dir, buffer, buflen);
358352
}
359353

360354
return true;
@@ -394,7 +388,7 @@ void hcd_dwc3_int_handler( uint8_t rhport, bool in_isr )
394388

395389
xhci_event_trb_t event_data;
396390

397-
event_data = get_xhc_event(&Usb3handle->xhci_priv);
391+
event_data = get_xhc_event(&xhci_handle);
398392

399393
trb_id =
400394
(xhci_event_trb_type_t) event_data.event_trb.trb_control_field.
@@ -420,14 +414,14 @@ void hcd_dwc3_int_handler( uint8_t rhport, bool in_isr )
420414

421415
/* RH port id should be always less than maximum supported port */
422416
if ((rh_params.rhport < 1U) || (rh_params.rhport >
423-
Usb3handle->xhci_priv.xhc_cap_ptr->hcsparams1_params.max_ports))
417+
xhci_handle.xhc_cap_ptr->hcsparams1_params.max_ports))
424418
{
425419
break;
426420
}
427421
if (rh_params.dev_attach_flag == 1)
428422
{
429423
hcd_event_device_attach(rh_params.rhport, in_isr);
430-
update_device_rh_params(&Usb3handle->xhci_priv, rh_params);
424+
update_device_rh_params(&xhci_handle, rh_params);
431425
}
432426
else if (rh_params.dev_attach_flag == -1)
433427
{
@@ -452,32 +446,30 @@ void hcd_dwc3_int_handler( uint8_t rhport, bool in_isr )
452446

453447
bool hcd_evaluate_xhci_context( void )
454448
{
455-
const uint16_t bcd_usb = Usb3handle->xhci_priv.usb_desc.dev_desc.bcdUSB;
449+
tusb_desc_device_t usb_desc;
450+
TU_ASSERT(tuh_descriptor_get_device_local(device_addr, &usb_desc));
451+
const uint16_t bcd_usb = usb_desc.bcdUSB;
456452
uint16_t desc_max_pkt_size;
457453
const uint16_t ep0_pkt_size =
458-
(Usb3handle->xhci_priv.ip_ctx->xe_context[ 0 ].xec_info &
454+
(xhci_handle.ip_ctx->xe_context[ 0 ].xec_info &
459455
~EP_CTX_MAX_PKT_SIZE_MSK) >> EP_CTX_MAX_PKT_SIZE_POS;
460456

461457
if (bcd_usb >= 0x300U)
462458
{
463-
desc_max_pkt_size = (uint16_t) pow(2,
464-
Usb3handle->xhci_priv.usb_desc.dev_desc.bMaxPacketSize0);
459+
desc_max_pkt_size = (uint16_t) pow(2, usb_desc.bMaxPacketSize0);
465460
}
466461
else
467462
{
468-
desc_max_pkt_size =
469-
Usb3handle->xhci_priv.usb_desc.dev_desc.bMaxPacketSize0;
463+
desc_max_pkt_size = usb_desc.bMaxPacketSize0;
470464
}
471465

472466
if (ep0_pkt_size != desc_max_pkt_size)
473467
{
474-
update_endpoint_packetsize(Usb3handle->xhci_priv.ip_ctx,
475-
desc_max_pkt_size);
468+
update_endpoint_packetsize(xhci_handle.ip_ctx, desc_max_pkt_size);
476469

477-
evaluate_endpoint(&Usb3handle->xhci_priv);
470+
evaluate_endpoint(&xhci_handle);
478471

479-
if (wait_for_command_completion_event(&Usb3handle->xhci_priv,
480-
EVALUATE_CONTEXT_CMD) != 0)
472+
if (wait_for_command_completion_event(&xhci_handle, EVALUATE_CONTEXT_CMD) != 0)
481473
{
482474
return false;
483475
}
@@ -489,7 +481,7 @@ bool hcd_evaluate_xhci_context( void )
489481
}
490482
bool hcd_dwc3_parse_full_conf_descriptor( tusb_desc_configuration_t *desc_cfg )
491483
{
492-
const uint8_t usb_speed = Usb3handle->xhci_priv.dev_data.dev_speed;
484+
const uint8_t usb_speed = xhci_handle.dev_data.dev_speed;
493485

494486
uint16_t const total_len = tu_le16toh(desc_cfg->wTotalLength);
495487
uint8_t const *desc_end = ((uint8_t const*) desc_cfg) + total_len;
@@ -554,8 +546,7 @@ bool hcd_dwc3_parse_full_conf_descriptor( tusb_desc_configuration_t *desc_cfg )
554546
ep_desc);
555547
}
556548

557-
if (xhci_parse_endpoint_descriptor(&Usb3handle->xhci_priv,
558-
&xhci_ep_desc) != 0)
549+
if (xhci_parse_endpoint_descriptor(&xhci_handle, &xhci_ep_desc) != 0)
559550
{
560551
ERROR("Failed to parse endpoint descriptor");
561552
return false;

src/portable/socfpga/dwc3/hcd_dwc3.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,11 @@
1717
#define XHCI_SPEED_SS (4)
1818
#define XHCI_QUEUE_SZ (10U)
1919

20-
typedef struct
21-
{
22-
/* reference to xHCI structure */
23-
struct xhci_data xhci_priv __attribute__((aligned(64)));
24-
}Usb3_Handle_t;
25-
26-
/*
27-
* @brief allocate all the usb3 port memory
28-
* @return
29-
* xHanlde reference to Usb3_Handle_t structure
30-
*/
31-
Usb3_Handle_t*alloc_usb_port(void);
32-
3320
/*
3421
* @brief deallocate all the usb3 port memory
3522
* @param[in] handle usb3 port reference handle
3623
*/
37-
void dealloc_usb_port(Usb3_Handle_t*handle);
24+
void dealloc_usb_port(struct xhci_data *xhci_handle);
3825

3926
/*
4027
* @brief api to check whether the reset process has completed or not

src/portable/socfpga/dwc3/socfpga_common.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,9 @@ OSAL_QUEUE_DEF(usbh_int_set, xhci_event_params, XHCI_QUEUE_SZ, xcc_event_t);
1717

1818
static QueueHandle_t xhci_queue;
1919

20-
Usb3_Handle_t* alloc_usb_port( void )
20+
void dealloc_usb_port( struct xhci_data *xhci_handle )
2121
{
22-
int ret;
23-
Usb3_Handle_t *xHandle = (Usb3_Handle_t*) pvPortAlignedAlloc(64,
24-
sizeof(Usb3_Handle_t));
25-
if ( xHandle == NULL )
26-
{
27-
ERROR("Cannot allocate memory");
28-
return NULL;
29-
}
30-
31-
ret = is_ptr_mem_aligned((uint64_t) xHandle, 64);
32-
if ( ret != 0 )
33-
{
34-
ERROR("Cannot allocate memory");
35-
return NULL;
36-
}
37-
38-
return xHandle;
39-
}
40-
41-
void dealloc_usb_port( Usb3_Handle_t *handle )
42-
{
43-
deallocate_xhci_context(&handle->xhci_priv);
22+
deallocate_xhci_context(xhci_handle);
4423
}
4524

4625
int init_hcd_params( void )

0 commit comments

Comments
 (0)