@@ -200,6 +200,62 @@ static void usbh_thread(void *p1, void *p2, void *p3)
200200 }
201201}
202202
203+ struct usb_device * host_connect_device (struct usbh_context * const ctx ,
204+ uint8_t device_speed )
205+ {
206+ int ret ;
207+ struct usb_device * udev ;
208+
209+ LOG_DBG ("Device connected event" );
210+
211+ /* Allocate new device for each connection */
212+ udev = usbh_device_alloc (ctx );
213+ if (udev == NULL ) {
214+ LOG_ERR ("Failed allocate new device" );
215+ return NULL ;
216+ }
217+
218+ udev -> state = USB_STATE_DEFAULT ;
219+ udev -> speed = device_speed ;
220+
221+ /* Add device to the udevs list */
222+ sys_dlist_append (& ctx -> udevs , & udev -> node );
223+
224+ if (usbh_device_init (udev )) {
225+ LOG_ERR ("Failed to reset new USB device" );
226+ /* Remove from list if initialization failed */
227+ sys_dlist_remove (& udev -> node );
228+ usbh_device_free (udev );
229+ return NULL ;
230+ }
231+
232+ ret = usbh_class_probe_all (udev );
233+ if (ret != 0 ) {
234+ LOG_ERR ("Failed to probe all classes for this new USB device" );
235+ }
236+
237+ LOG_INF ("Device connected: addr=%d, speed=%d, total devices=%zu" ,
238+ udev -> addr , udev -> speed , sys_dlist_len (& ctx -> udevs ));
239+
240+ return udev ;
241+ }
242+
243+ void usbh_device_disconnected (struct usbh_context * ctx , struct usb_device * udev )
244+ {
245+ if (!ctx || !udev ) {
246+ return ;
247+ }
248+
249+ usbh_class_remove_all (udev );
250+
251+ k_mutex_lock (& ctx -> mutex , K_FOREVER );
252+ sys_dlist_remove (& udev -> node );
253+ k_mutex_unlock (& ctx -> mutex );
254+
255+ usbh_device_free (udev );
256+ LOG_DBG ("Device removed" );
257+ }
258+
203259int usbh_init_device_intl (struct usbh_context * const uhs_ctx )
204260{
205261 int ret ;
0 commit comments