4646static void hcd_xhci_set_configuration ();
4747static void hcd_dwc3_update_device_address ( uint8_t daddr );
4848
49- Usb3_Handle_t * Usb3handle ;
49+ struct xhci_data xhci_handle __attribute__(( aligned ( 64 ))) ;
5050uint8_t device_addr = 0 ;
5151
5252static 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 *--------------------------------------------------------------------+*/
134130bool 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
144139void hcd_dwc3_port_reset ( uint8_t rhport )
145140{
146-
147141 (void ) rhport ;
148142 reset_usb_port (rhport );
149143}
150144
151145static 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
202196static void hcd_dwc3_update_device_address ( uint8_t daddr )
203197{
204- device_addr = 1U ;
198+ device_addr = daddr ;
205199}
206200
207201void 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 *--------------------------------------------------------------------+*/
279273static 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
453447bool 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}
490482bool 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;
0 commit comments