tinyusb  0.4
Click here to lend your support to tinyusb donation and make a donation at pledgie.com
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
usbh.c
1 /**************************************************************************/
37 /**************************************************************************/
38 
39 #include "common/common.h"
40 
41 #if MODE_HOST_SUPPORTED
42 
43 #define _TINY_USB_SOURCE_FILE_
44 
45 //--------------------------------------------------------------------+
46 // INCLUDE
47 //--------------------------------------------------------------------+
48 #include "tusb.h"
49 #include "hub.h"
50 #include "usbh_hcd.h"
51 
52 //--------------------------------------------------------------------+
53 // MACRO CONSTANT TYPEDEF
54 //--------------------------------------------------------------------+
55 static host_class_driver_t const usbh_class_drivers[] =
56 {
57  #if HOST_CLASS_HID
58  [TUSB_CLASS_HID] = {
59  .init = hidh_init,
60  .open_subtask = hidh_open_subtask,
61  .isr = hidh_isr,
62  .close = hidh_close
63  },
64  #endif
65 
66  #if TUSB_CFG_HOST_CDC
67  [TUSB_CLASS_CDC] = {
68  .init = cdch_init,
69  .open_subtask = cdch_open_subtask,
70  .isr = cdch_isr,
71  .close = cdch_close
72  },
73  #endif
74 
75  #if TUSB_CFG_HOST_MSC
76  [TUSB_CLASS_MSC] = {
77  .init = msch_init,
78  .open_subtask = msch_open_subtask,
79  .isr = msch_isr,
80  .close = msch_close
81  },
82  #endif
83 
84  #if TUSB_CFG_HOST_HUB
85  [TUSB_CLASS_HUB] = {
86  .init = hub_init,
87  .open_subtask = hub_open_subtask,
88  .isr = hub_isr,
89  .close = hub_close
90  },
91  #endif
92 
93  #if TUSB_CFG_HOST_CUSTOM_CLASS
94  [TUSB_CLASS_MAPPED_INDEX_END-1] = {
95  .init = cush_init,
96  .open_subtask = cush_open_subtask,
97  .isr = cush_isr,
98  .close = cush_close
99  }
100  #endif
101 };
102 
103 enum { USBH_CLASS_DRIVER_COUNT = sizeof(usbh_class_drivers) / sizeof(host_class_driver_t) };
104 
105 //--------------------------------------------------------------------+
106 // INTERNAL OBJECT & FUNCTION DECLARATION
107 //--------------------------------------------------------------------+
108 TUSB_CFG_ATTR_USBRAM usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; // including zero-address
109 
110 //------------- Enumeration Task Data -------------/
111 enum { ENUM_QUEUE_DEPTH = 5 };
112 OSAL_TASK_DEF(usbh_enumeration_task, 200, TUSB_CFG_OS_TASK_PRIO);
113 OSAL_QUEUE_DEF(enum_queue_def, ENUM_QUEUE_DEPTH, uint32_t);
114 
115 static osal_queue_handle_t enum_queue_hdl;
116 TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(4) STATIC_VAR uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
117 
118 //------------- Reporter Task Data -------------//
119 
120 //------------- Helper Function Prototypes -------------//
121 static inline uint8_t get_new_address(void) ATTR_ALWAYS_INLINE;
122 static inline uint8_t get_configure_number_for_device(tusb_descriptor_device_t* dev_desc) ATTR_ALWAYS_INLINE;
123 
124 //--------------------------------------------------------------------+
125 // PUBLIC API (Parameter Verification is required)
126 //--------------------------------------------------------------------+
127 tusb_device_state_t tusbh_device_get_state (uint8_t const dev_addr)
128 {
129  ASSERT_INT_WITHIN(1, TUSB_CFG_HOST_DEVICE_MAX, dev_addr, TUSB_DEVICE_STATE_INVALID_PARAMETER);
130  return (tusb_device_state_t) usbh_devices[dev_addr].state;
131 }
132 
133 uint32_t tusbh_device_get_mounted_class_flag(uint8_t dev_addr)
134 {
135  return tusbh_device_is_configured(dev_addr) ? usbh_devices[dev_addr].flag_supported_class : 0;
136 }
137 
138 //--------------------------------------------------------------------+
139 // CLASS-USBD API (don't require to verify parameters)
140 //--------------------------------------------------------------------+
141 tusb_error_t usbh_init(void)
142 {
143  memclr_(usbh_devices, sizeof(usbh_device_info_t)*(TUSB_CFG_HOST_DEVICE_MAX+1));
144 
145  ASSERT_STATUS( hcd_init() );
146 
147  //------------- Enumeration & Reporter Task init -------------//
148  enum_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(enum_queue_def) );
149  ASSERT_PTR(enum_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED);
150  ASSERT_STATUS( osal_task_create( OSAL_TASK_REF(usbh_enumeration_task) ));
151 
152  //------------- Semaphore, Mutex for Control Pipe -------------//
153  for(uint8_t i=0; i<TUSB_CFG_HOST_DEVICE_MAX+1; i++) // including address zero
154  {
155  usbh_device_info_t * const p_device = &usbh_devices[i];
156 
157  p_device->control.sem_hdl = osal_semaphore_create( OSAL_SEM_REF(p_device->control.semaphore) );
158  ASSERT_PTR(p_device->control.sem_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED);
159 
160  p_device->control.mutex_hdl = osal_mutex_create ( OSAL_MUTEX_REF(p_device->control.mutex) );
161  ASSERT_PTR(p_device->control.mutex_hdl, TUSB_ERROR_OSAL_MUTEX_FAILED);
162  }
163 
164  //------------- class init -------------//
165  for (uint8_t class_index = 1; class_index < USBH_CLASS_DRIVER_COUNT; class_index++)
166  {
167  if (usbh_class_drivers[class_index].init)
168  {
169  usbh_class_drivers[class_index].init();
170  }
171  }
172 
173  return TUSB_ERROR_NONE;
174 }
175 
176 //------------- USBH control transfer -------------//
177 // function called within a task, requesting os blocking services, subtask input parameter must be static/global variables or constant
178 tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest,
179  uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t* data)
180 {
181  static tusb_error_t error; // FIXME [CMSIS-RTX] use svc for OS API, error value changed after mutex release at the end of function
182 
183  OSAL_SUBTASK_BEGIN
184 
185  osal_mutex_wait(usbh_devices[dev_addr].control.mutex_hdl, OSAL_TIMEOUT_NORMAL, &error);
186  SUBTASK_ASSERT_STATUS_WITH_HANDLER(error, osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl));
187 
188  usbh_devices[dev_addr].control.request = (tusb_control_request_t) {
189  {.bmRequestType = bmRequestType},
190  .bRequest = bRequest,
191  .wValue = wValue,
192  .wIndex = wIndex,
193  .wLength = wLength
194  };
195 
196 #ifndef _TEST_
197  usbh_devices[dev_addr].control.pipe_status = 0;
198 #else
199  usbh_devices[dev_addr].control.pipe_status = TUSB_EVENT_XFER_COMPLETE; // in Test project, mark as complete immediately
200 #endif
201 
202  SUBTASK_ASSERT_STATUS_WITH_HANDLER( hcd_pipe_control_xfer(dev_addr, &usbh_devices[dev_addr].control.request, data),
203  osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl) );
204 
205  osal_semaphore_wait(usbh_devices[dev_addr].control.sem_hdl, OSAL_TIMEOUT_NORMAL, &error); // careful of local variable without static
206  osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl);
207 
208  // TODO make handler for this function general purpose
209  if (TUSB_ERROR_NONE != error) SUBTASK_EXIT(error);
210  if (TUSB_EVENT_XFER_STALLED == usbh_devices[dev_addr].control.pipe_status) SUBTASK_EXIT(TUSB_ERROR_USBH_XFER_STALLED);
211  if (TUSB_EVENT_XFER_ERROR == usbh_devices[dev_addr].control.pipe_status) SUBTASK_EXIT(TUSB_ERROR_USBH_XFER_FAILED);
212 
213 // SUBTASK_ASSERT_WITH_HANDLER(TUSB_ERROR_NONE == error &&
214 // TUSB_EVENT_XFER_COMPLETE == usbh_devices[dev_addr].control.pipe_status,
215 // tusbh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) );
216 
217  OSAL_SUBTASK_END
218 }
219 
220 tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) ATTR_ALWAYS_INLINE;
221 tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size)
222 {
223  osal_semaphore_reset( usbh_devices[dev_addr].control.sem_hdl );
224  osal_mutex_reset( usbh_devices[dev_addr].control.mutex_hdl );
225 
226  ASSERT_STATUS( hcd_pipe_control_open(dev_addr, max_packet_size) );
227 
228  return TUSB_ERROR_NONE;
229 }
230 
231 static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) ATTR_ALWAYS_INLINE;
232 static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr)
233 {
234  ASSERT_STATUS( hcd_pipe_control_close(dev_addr) );
235 
236  return TUSB_ERROR_NONE;
237 }
238 
239 // TODO [USBH] unify pipe status get
240 //tusb_interface_status_t usbh_pipe_status_get(pipe_handle_t pipe_hdl)
241 //{
242 // return TUSB_INTERFACE_STATUS_BUSY;
243 //}
244 
245 //--------------------------------------------------------------------+
246 // USBH-HCD ISR/Callback API
247 //--------------------------------------------------------------------+
248 // interrupt caused by a TD (with IOC=1) in pipe of class class_code
249 void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, tusb_event_t event, uint32_t xferred_bytes)
250 {
251  uint8_t class_index = std_class_code_to_index(class_code);
252  if (TUSB_XFER_CONTROL == pipe_hdl.xfer_type)
253  {
254  usbh_devices[ pipe_hdl.dev_addr ].control.pipe_status = event;
255 // usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary
256  osal_semaphore_post( usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl );
257  }else if (usbh_class_drivers[class_index].isr)
258  {
259  usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes);
260  }else
261  {
262  ASSERT(false, VOID_RETURN); // something wrong, no one claims the isr's source
263  }
264 }
265 
266 void usbh_hub_port_plugged_isr(uint8_t hub_addr, uint8_t hub_port)
267 {
268  osal_queue_send(enum_queue_hdl,
269  &(usbh_enumerate_t){
270  .core_id = usbh_devices[hub_addr].core_id,
271  .hub_addr = hub_addr,
272  .hub_port = hub_port}
273  );
274 }
275 
276 void usbh_hcd_rhport_plugged_isr(uint8_t hostid)
277 {
278  osal_queue_send(enum_queue_hdl,
279  &(usbh_enumerate_t){
280  .core_id = hostid,
281  .hub_addr = 0,
282  .hub_port = 0}
283  );
284 }
285 
286 // a device unplugged on hostid, hub_addr, hub_port
287 // return true if found and unmounted device, false if cannot find
288 static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_port)
289 {
290  bool is_found = false;
291  //------------- find the all devices (star-network) under port that is unplugged -------------//
292  for (uint8_t dev_addr = 0; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr ++)
293  {
294  if (usbh_devices[dev_addr].core_id == hostid &&
295  (hub_addr == 0 || usbh_devices[dev_addr].hub_addr == hub_addr) && // hub_addr == 0 & hub_port == 0 means roothub
296  (hub_port == 0 || usbh_devices[dev_addr].hub_port == hub_port) &&
297  usbh_devices[dev_addr].state != TUSB_DEVICE_STATE_UNPLUG)
298  {
299  // TODO Hub multiple level
300  for (uint8_t class_index = 1; class_index < USBH_CLASS_DRIVER_COUNT; class_index++)
301  {
302  if ((usbh_devices[dev_addr].flag_supported_class & BIT_(class_index)) &&
303  usbh_class_drivers[class_index].close)
304  {
305  usbh_class_drivers[class_index].close(dev_addr);
306  }
307  }
308 
309  // TODO refractor
310  // set to REMOVING to allow HCD to clean up its cached data for this device
311  // HCD must set this device's state to TUSB_DEVICE_STATE_UNPLUG when done
312  usbh_devices[dev_addr].state = TUSB_DEVICE_STATE_REMOVING;
313  usbh_devices[dev_addr].flag_supported_class = 0;
314 
315  usbh_pipe_control_close(dev_addr);
316 
317 
318  is_found = true;
319  }
320  }
321 
322  if (is_found) hcd_port_unplug(usbh_devices[0].core_id); // TODO hack
323 
324 }
325 
326 void usbh_hcd_rhport_unplugged_isr(uint8_t hostid)
327 {
328  osal_queue_send(enum_queue_hdl,
330  {
331  .core_id = hostid,
332  .hub_addr = 0,
333  .hub_port = 0
334  } );
335 }
336 
337 //--------------------------------------------------------------------+
338 // ENUMERATION TASK
339 //--------------------------------------------------------------------+
340 static tusb_error_t enumeration_body_subtask(void);
341 
342 // To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper
343 // and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with
344 // forever loop cannot have any return at all.
345 OSAL_TASK_FUNCTION(usbh_enumeration_task, p_task_para)
346 {
347  (void) p_task_para; // suppress compiler warnings
348 
349  OSAL_TASK_LOOP_BEGIN
350 
351  enumeration_body_subtask();
352 
353  OSAL_TASK_LOOP_END
354 }
355 
356 tusb_error_t enumeration_body_subtask(void)
357 {
358  enum {
359  POWER_STABLE_DELAY = 300,
360  RESET_DELAY = 100 // NXP's EHCI require more than 50ms to work properly although the USB specs say only 50ms
361  };
362 
363  tusb_error_t error;
364  usbh_enumerate_t enum_entry;
365 
366  // for OSAL_NONE local variable won't retain value after blocking service sem_wait/queue_recv
367  static uint8_t new_addr;
368  static uint8_t configure_selected = 1; // TODO move
369  static uint8_t *p_desc = NULL; // TODO move
370 
371  OSAL_SUBTASK_BEGIN
372 
373  osal_queue_receive(enum_queue_hdl, &enum_entry, OSAL_TIMEOUT_WAIT_FOREVER, &error);
374  SUBTASK_ASSERT_STATUS(error);
375 
376  usbh_devices[0].core_id = enum_entry.core_id; // TODO refractor integrate to device_pool
377  usbh_devices[0].hub_addr = enum_entry.hub_addr;
378  usbh_devices[0].hub_port = enum_entry.hub_port;
379  usbh_devices[0].state = TUSB_DEVICE_STATE_UNPLUG;
380 
381  //------------- connected/disconnected directly with roothub -------------//
382  if ( usbh_devices[0].hub_addr == 0)
383  {
384  if( hcd_port_connect_status(usbh_devices[0].core_id) )
385  { // connection event
386  osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get
387 
388  if ( !hcd_port_connect_status(usbh_devices[0].core_id) ) SUBTASK_EXIT(TUSB_ERROR_NONE); // exit if device unplugged while delaying
389 
390  hcd_port_reset( usbh_devices[0].core_id ); // port must be reset to have correct speed operation
391  osal_task_delay(RESET_DELAY);
392 
393  usbh_devices[0].speed = hcd_port_speed_get( usbh_devices[0].core_id );
394  }
395  else
396  { // disconnection event
397  usbh_device_unplugged(usbh_devices[0].core_id, 0, 0);
398  SUBTASK_EXIT(TUSB_ERROR_NONE); // restart task
399  }
400  }
401  #if TUSB_CFG_HOST_HUB
402  //------------- connected/disconnected via hub -------------//
403  else
404  {
405  //------------- Get Port Status -------------//
406  OSAL_SUBTASK_INVOKED_AND_WAIT(
407  usbh_control_xfer_subtask( usbh_devices[0].hub_addr, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_CLASS, TUSB_REQUEST_RECIPIENT_OTHER),
408  HUB_REQUEST_GET_STATUS, 0, usbh_devices[0].hub_port,
409  4, enum_data_buffer ),
410  error
411  );
412 // SUBTASK_ASSERT_STATUS( error );
413  SUBTASK_ASSERT_STATUS_WITH_HANDLER(error, hub_status_pipe_queue( usbh_devices[0].hub_addr) ); // TODO hub refractor
414 
415  // Acknowledge Port Connection Change
416  OSAL_SUBTASK_INVOKED_AND_WAIT( hub_port_clear_feature_subtask(usbh_devices[0].hub_addr, usbh_devices[0].hub_port, HUB_FEATURE_PORT_CONNECTION_CHANGE), error );
417 
418  hub_port_status_response_t * p_port_status;
419  p_port_status = ((hub_port_status_response_t *) enum_data_buffer);
420 
421  if ( ! p_port_status->status_change.connect_status ) SUBTASK_EXIT(TUSB_ERROR_NONE); // only handle connection change
422 
423  if ( ! p_port_status->status_current.connect_status )
424  { // Disconnection event
425  usbh_device_unplugged(usbh_devices[0].core_id, usbh_devices[0].hub_addr, usbh_devices[0].hub_port);
426 
427  (void) hub_status_pipe_queue( usbh_devices[0].hub_addr ); // done with hub, waiting for next data on status pipe
428  SUBTASK_EXIT(TUSB_ERROR_NONE); // restart task
429  }
430  else
431  { // Connection Event
432  OSAL_SUBTASK_INVOKED_AND_WAIT ( hub_port_reset_subtask(usbh_devices[0].hub_addr, usbh_devices[0].hub_port), error );
433 // SUBTASK_ASSERT_STATUS( error );
434  SUBTASK_ASSERT_STATUS_WITH_HANDLER(error, hub_status_pipe_queue( usbh_devices[0].hub_addr) ); // TODO hub refractor
435 
436  usbh_devices[0].speed = hub_port_get_speed();
437 
438  // Acknowledge Port Reset Change
439  OSAL_SUBTASK_INVOKED_AND_WAIT( hub_port_clear_feature_subtask(usbh_devices[0].hub_addr, usbh_devices[0].hub_port, HUB_FEATURE_PORT_RESET_CHANGE), error );
440  }
441  }
442  #endif
443 
444  SUBTASK_ASSERT_STATUS( usbh_pipe_control_open(0, 8) );
445  usbh_devices[0].state = TUSB_DEVICE_STATE_ADDRESSED;
446 
447  //------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
448  OSAL_SUBTASK_INVOKED_AND_WAIT(
449  usbh_control_xfer_subtask( 0, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
450  TUSB_REQUEST_GET_DESCRIPTOR, (TUSB_DESC_TYPE_DEVICE << 8), 0,
451  8, enum_data_buffer ),
452  error
453  );
454 
455  //------------- Reset device again before Set Address -------------//
456  if (usbh_devices[0].hub_addr == 0)
457  { // connected directly to roothub
458  SUBTASK_ASSERT_STATUS(error); // TODO some slow device is observed to fail the very fist controller xfer, can try more times
459  hcd_port_reset( usbh_devices[0].core_id ); // reset port after 8 byte descriptor
460  osal_task_delay(RESET_DELAY);
461  }
462  #if TUSB_CFG_HOST_HUB
463  else
464  { // connected via a hub
465  SUBTASK_ASSERT_STATUS_WITH_HANDLER(error, hub_status_pipe_queue( usbh_devices[0].hub_addr) ); // TODO hub refractor
466  OSAL_SUBTASK_INVOKED_AND_WAIT ( hub_port_reset_subtask(usbh_devices[0].hub_addr, usbh_devices[0].hub_port), error );
467 
468  if ( TUSB_ERROR_NONE == error )
469  { // Acknowledge Port Reset Change if Reset Successful
470  OSAL_SUBTASK_INVOKED_AND_WAIT( hub_port_clear_feature_subtask(usbh_devices[0].hub_addr, usbh_devices[0].hub_port, HUB_FEATURE_PORT_RESET_CHANGE), error );
471  }
472 
473  (void) hub_status_pipe_queue( usbh_devices[0].hub_addr ); // done with hub, waiting for next data on status pipe
474  }
475  #endif
476 
477  //------------- Set new address -------------//
478  new_addr = get_new_address();
479  SUBTASK_ASSERT(new_addr <= TUSB_CFG_HOST_DEVICE_MAX); // TODO notify application we reach max devices
480 
481  OSAL_SUBTASK_INVOKED_AND_WAIT(
482  usbh_control_xfer_subtask( 0, bm_request_type(TUSB_DIR_HOST_TO_DEV, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
483  TUSB_REQUEST_SET_ADDRESS, new_addr, 0,
484  0, NULL ),
485  error
486  );
487  SUBTASK_ASSERT_STATUS(error);
488 
489  //------------- update port info & close control pipe of addr0 -------------//
490  usbh_devices[new_addr].core_id = usbh_devices[0].core_id;
491  usbh_devices[new_addr].hub_addr = usbh_devices[0].hub_addr;
492  usbh_devices[new_addr].hub_port = usbh_devices[0].hub_port;
493  usbh_devices[new_addr].speed = usbh_devices[0].speed;
494  usbh_devices[new_addr].state = TUSB_DEVICE_STATE_ADDRESSED;
495 
496  usbh_pipe_control_close(0);
497  usbh_devices[0].state = TUSB_DEVICE_STATE_UNPLUG;
498 
499  // open control pipe for new address
500  SUBTASK_ASSERT_STATUS ( usbh_pipe_control_open(new_addr, ((tusb_descriptor_device_t*) enum_data_buffer)->bMaxPacketSize0 ) );
501 
502  //------------- Get full device descriptor -------------//
503  OSAL_SUBTASK_INVOKED_AND_WAIT(
504  usbh_control_xfer_subtask( new_addr, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
505  TUSB_REQUEST_GET_DESCRIPTOR, (TUSB_DESC_TYPE_DEVICE << 8), 0,
506  18, enum_data_buffer ),
507  error
508  );
509  SUBTASK_ASSERT_STATUS(error);
510 
511  // update device info TODO alignment issue
512  usbh_devices[new_addr].vendor_id = ((tusb_descriptor_device_t*) enum_data_buffer)->idVendor;
513  usbh_devices[new_addr].product_id = ((tusb_descriptor_device_t*) enum_data_buffer)->idProduct;
514  usbh_devices[new_addr].configure_count = ((tusb_descriptor_device_t*) enum_data_buffer)->bNumConfigurations;
515 
516  configure_selected = get_configure_number_for_device((tusb_descriptor_device_t*) enum_data_buffer);
517  SUBTASK_ASSERT(configure_selected <= usbh_devices[new_addr].configure_count); // TODO notify application when invalid configuration
518 
519  //------------- Get 9 bytes of configuration descriptor -------------//
520  OSAL_SUBTASK_INVOKED_AND_WAIT(
521  usbh_control_xfer_subtask( new_addr, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
522  TUSB_REQUEST_GET_DESCRIPTOR, (TUSB_DESC_TYPE_CONFIGURATION << 8) | (configure_selected - 1), 0,
523  9, enum_data_buffer ),
524  error
525  );
526  SUBTASK_ASSERT_STATUS(error);
527  SUBTASK_ASSERT_WITH_HANDLER( TUSB_CFG_HOST_ENUM_BUFFER_SIZE >= ((tusb_descriptor_configuration_t*)enum_data_buffer)->wTotalLength,
528  tusbh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_CONFIG_DESC_TOO_LONG, NULL) );
529 
530  //------------- Get full configuration descriptor -------------//
531  OSAL_SUBTASK_INVOKED_AND_WAIT(
532  usbh_control_xfer_subtask( new_addr, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
533  TUSB_REQUEST_GET_DESCRIPTOR, (TUSB_DESC_TYPE_CONFIGURATION << 8) | (configure_selected - 1), 0,
534  TUSB_CFG_HOST_ENUM_BUFFER_SIZE, enum_data_buffer ),
535  error
536  );
537  SUBTASK_ASSERT_STATUS(error);
538 
539  // update configuration info
540  usbh_devices[new_addr].interface_count = ((tusb_descriptor_configuration_t*) enum_data_buffer)->bNumInterfaces;
541 
542  //------------- Set Configure -------------//
543  OSAL_SUBTASK_INVOKED_AND_WAIT(
544  usbh_control_xfer_subtask( new_addr, bm_request_type(TUSB_DIR_HOST_TO_DEV, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_DEVICE),
545  TUSB_REQUEST_SET_CONFIGURATION, configure_selected, 0,
546  0, NULL ),
547  error
548  );
549  SUBTASK_ASSERT_STATUS(error);
550 
551  usbh_devices[new_addr].state = TUSB_DEVICE_STATE_CONFIGURED;
552 
553  //------------- TODO Get String Descriptors -------------//
554 
555  //------------- parse configuration & install drivers -------------//
556  p_desc = enum_data_buffer + sizeof(tusb_descriptor_configuration_t);
557 
558  // parse each interfaces
559  while( p_desc < enum_data_buffer + ((tusb_descriptor_configuration_t*)enum_data_buffer)->wTotalLength )
560  {
561  // skip until we see interface descriptor
562  if ( TUSB_DESC_TYPE_INTERFACE != p_desc[DESCRIPTOR_OFFSET_TYPE] )
563  {
564  p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // skip the descriptor, increase by the descriptor's length
565  }else
566  {
567  static uint8_t class_index; // has to be static as it is used to call class's open_subtask
568 
569  class_index = std_class_code_to_index( ((tusb_descriptor_interface_t*) p_desc)->bInterfaceClass );
570  SUBTASK_ASSERT( class_index != 0 ); // class_index == 0 means corrupted data, abort enumeration
571 
572  if (usbh_class_drivers[class_index].open_subtask &&
573  !(class_index == TUSB_CLASS_HUB && usbh_devices[new_addr].hub_addr != 0))
574  { // supported class, TODO Hub disable multiple level
575  static uint16_t length;
576  length = 0;
577 
578  OSAL_SUBTASK_INVOKED_AND_WAIT ( // parameters in task/sub_task must be static storage (static or global)
579  usbh_class_drivers[class_index].open_subtask( new_addr, (tusb_descriptor_interface_t*) p_desc, &length ),
580  error
581  );
582 
583  if (error == TUSB_ERROR_NONE)
584  {
585  SUBTASK_ASSERT( length >= sizeof(tusb_descriptor_interface_t) );
586  usbh_devices[new_addr].flag_supported_class |= BIT_(class_index);
587  p_desc += length;
588  }else // Interface open failed, for example a subclass is not supported
589  {
590  p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // skip this interface, the rest will be skipped by the above loop
591  }
592  } else // unsupported class (not enable or yet implemented)
593  {
594  p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // skip this interface, the rest will be skipped by the above loop
595  }
596  }
597  }
598 
599  tusbh_device_mount_succeed_cb(new_addr);
600 
601  OSAL_SUBTASK_END
602 }
603 
604 //--------------------------------------------------------------------+
605 // REPORTER TASK & ITS DATA
606 //--------------------------------------------------------------------+
607 
608 
609 
610 
611 
612 //--------------------------------------------------------------------+
613 // INTERNAL HELPER
614 //--------------------------------------------------------------------+
615 static inline uint8_t get_new_address(void)
616 {
617  uint8_t addr;
618  for (addr=1; addr <= TUSB_CFG_HOST_DEVICE_MAX; addr++)
619  {
620  if (usbh_devices[addr].state == TUSB_DEVICE_STATE_UNPLUG)
621  break;
622  }
623  return addr;
624 }
625 
626 static inline uint8_t get_configure_number_for_device(tusb_descriptor_device_t* dev_desc)
627 {
628  uint8_t config_num = 1;
629 
630  // invoke callback to ask user which configuration to select
631  if (tusbh_device_attached_cb)
632  {
633  config_num = min8_of(1, tusbh_device_attached_cb(dev_desc) );
634  }
635 
636  return config_num;
637 }
638 
639 #endif
#define BIT_(n)
n-th Bit
Definition: binary.h:54
USB Standard Interface Descriptor (section 9.6.1 table 9-12)
bool hcd_port_connect_status(uint8_t hostid) ATTR_PURE ATTR_WARN_UNUSED_RESULT
return the current connect status of roothub port
#define TUSB_CFG_HOST_ENUM_BUFFER_SIZE
Buffer size used for getting device configuration descriptor. You may want to increase this from defa...
#define TUSB_CFG_HOST_DEVICE_MAX
Maximum number of device host stack can manage If hub class is not enabled, set this equal to numbe...
tusb_device_state_t
Device State.
Definition: tusb_types.h:163
#define TUSB_CFG_ATTR_USBRAM
USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */.
#define TUSB_CFG_OS_TASK_PRIO
If TUSB_CFG_OS is configured to use a real RTOS (other than TUSB_OS_NONE). This determines the priori...
tusb_error_t
Error Code returned.
Definition: tusb_errors.h:100
#define ATTR_ALIGNED(Bytes)
This attribute specifies a minimum alignment for the variable or structure field, measured in bytes...
Definition: compiler_gcc.h:72
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
Definition: compiler_gcc.h:89
USB Standard Device Descriptor (section 9.6.1, table 9-8)