From 3dc1ac91d639a9483300318e375a2906aab01862 Mon Sep 17 00:00:00 2001 From: p-szafonimateusz Date: Mon, 9 Dec 2024 14:25:24 +0100 Subject: [PATCH] usbhost: add common usb host waiter and drivers initialization add common usb host waiter and drivers initialization which is required for xHCI support Signed-off-by: p-szafonimateusz --- drivers/drivers_initialize.c | 5 ++ drivers/usbhost/CMakeLists.txt | 4 + drivers/usbhost/Kconfig | 22 ++++++ drivers/usbhost/Make.defs | 4 + drivers/usbhost/usbhost_drivers.c | 118 ++++++++++++++++++++++++++++++ drivers/usbhost/usbhost_waiter.c | 105 ++++++++++++++++++++++++++ include/nuttx/usb/usbhost.h | 23 ++++++ 7 files changed, 281 insertions(+) create mode 100644 drivers/usbhost/usbhost_drivers.c create mode 100644 drivers/usbhost/usbhost_waiter.c diff --git a/drivers/drivers_initialize.c b/drivers/drivers_initialize.c index 9c74301f49..43b85aa420 100644 --- a/drivers/drivers_initialize.c +++ b/drivers/drivers_initialize.c @@ -55,6 +55,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -268,6 +269,10 @@ void drivers_initialize(void) mtd_loop_register(); #endif +#ifdef CONFIG_USBHOST_WAITER + usbhost_drivers_initialize(); +#endif + #if defined(CONFIG_PCI) && !defined(CONFIG_PCI_LATE_DRIVERS_REGISTER) pci_register_drivers(); #endif diff --git a/drivers/usbhost/CMakeLists.txt b/drivers/usbhost/CMakeLists.txt index ec6373286f..b38f7d7858 100644 --- a/drivers/usbhost/CMakeLists.txt +++ b/drivers/usbhost/CMakeLists.txt @@ -27,6 +27,10 @@ if(CONFIG_USBHOST) set(SRCS usbhost_registry.c usbhost_registerclass.c usbhost_findclass.c usbhost_enumerate.c usbhost_devaddr.c) + if(CONFIG_USBHOST_WAITER) + list(APPEND SRCS usbhost_waiter.c usbhost_drivers.c) + endif() + if(CONFIG_USBHOST_HUB) list(APPEND SRCS usbhost_hub.c) endif() diff --git a/drivers/usbhost/Kconfig b/drivers/usbhost/Kconfig index f0ee0beec3..28e64ffc85 100644 --- a/drivers/usbhost/Kconfig +++ b/drivers/usbhost/Kconfig @@ -51,6 +51,28 @@ config USBHOST_ASYNCH I/O transfer. This may be required, for example, to receive infrequent, asynchronous input from an interrupt pipe. +config USBHOST_WAITER + bool "USB Host Waiter Support" + default n + ---help--- + Select this option to build in support for common USB host waiter. + +if USBHOST_WAITER + +config USBHOST_WAITER_PRIO + int "USB Host Waiter Thread Priority" + default 100 + ---help--- + USB Host Waiter thread priority. + +config USBHOST_WAITER_STACKSIZE + int "USB Host Waiter Thread Stack Size" + default DEFAULT_TASK_STACKSIZE + ---help--- + USB Host Waiter thread stack size + +endif # USBHOST_WAITER + config USBHOST_HUB bool "USB Hub Support" default n diff --git a/drivers/usbhost/Make.defs b/drivers/usbhost/Make.defs index a226d61495..38e34c2add 100644 --- a/drivers/usbhost/Make.defs +++ b/drivers/usbhost/Make.defs @@ -28,6 +28,10 @@ ifeq ($(CONFIG_USBHOST),y) CSRCS += usbhost_registry.c usbhost_registerclass.c usbhost_findclass.c CSRCS += usbhost_enumerate.c usbhost_devaddr.c +ifeq ($(CONFIG_USBHOST_WAITER),y) +CSRCS += usbhost_waiter.c usbhost_drivers.c +endif + ifeq ($(CONFIG_USBHOST_HUB),y) CSRCS += usbhost_hub.c endif diff --git a/drivers/usbhost/usbhost_drivers.c b/drivers/usbhost/usbhost_drivers.c new file mode 100644 index 0000000000..b0c5851fb3 --- /dev/null +++ b/drivers/usbhost/usbhost_drivers.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * drivers/usbhost/usbhost_drivers.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: usbhost_waiter_initialize + * + * Description: + * Initialize all enabled USB host device drivers. + * + ****************************************************************************/ + +void usbhost_drivers_initialize(void) +{ + int ret; + + UNUSED(ret); + + /* First, register all of the class drivers needed to support the drivers + * that we care about: + */ + + uinfo("Register class drivers\n"); + +#ifdef CONFIG_USBHOST_HUB + /* Initialize USB hub class support */ + + ret = usbhost_hub_initialize(); + if (ret < 0) + { + uerr("ERROR: usbhost_hub_initialize failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_USBHOST_MSC + /* Register the USB mass storage class */ + + ret = usbhost_msc_initialize(); + if (ret != OK) + { + uerr("ERROR: Failed to register the mass storage class: %d\n", ret); + } +#endif + +#ifdef CONFIG_USBHOST_CDCACM + /* Register the CDC/ACM serial class */ + + ret = usbhost_cdcacm_initialize(); + if (ret != OK) + { + uerr("ERROR: Failed to register the CDC/ACM serial class: %d\n", ret); + } +#endif + +#ifdef CONFIG_USBHOST_HIDKBD + /* Initialize the HID keyboard class */ + + ret = usbhost_kbdinit(); + if (ret != OK) + { + uerr("ERROR: Failed to register the HID keyboard class\n"); + } +#endif + +#ifdef CONFIG_USBHOST_HIDMOUSE + /* Initialize the HID mouse class */ + + ret = usbhost_mouse_init(); + if (ret != OK) + { + uerr("ERROR: Failed to register the HID mouse class\n"); + } +#endif + +#ifdef CONFIG_USBHOST_FT232R + /* Initialize the FT232R class */ + + ret = usbhost_ft232r_initialize(); + if (ret != OK) + { + uerr("ERROR: Failed to register the FT232R class\n"); + } +#endif +} diff --git a/drivers/usbhost/usbhost_waiter.c b/drivers/usbhost/usbhost_waiter.c new file mode 100644 index 0000000000..043d88f445 --- /dev/null +++ b/drivers/usbhost/usbhost_waiter.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * drivers/usbhost/usbhost_waiter.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: usbhost_waiter + * + * Description: + * Wait for USB devices to be connected. + * + ****************************************************************************/ + +static int usbhost_waiter(int argc, FAR char *argv[]) +{ + FAR struct usbhost_connection_s *conn = + (FAR struct usbhost_connection_s *)((uintptr_t)strtoul(argv[1], + NULL, 16)); + FAR struct usbhost_hubport_s *hport; + + uinfo("Running %p\n", conn); + for (; ; ) + { + /* Wait for the device to change state */ + + DEBUGVERIFY(CONN_WAIT(conn, &hport)); + uinfo("%s\n", hport->connected ? "connected" : "disconnected"); + + /* Did we just become connected? */ + + if (hport->connected) + { + /* Yes.. enumerate the newly connected device */ + + CONN_ENUMERATE(conn, hport); + } + } + + /* Keep the compiler from complaining */ + + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: usbhost_waiter_initialize + * + * Description: + * Initialize the USB host waiter. This function will start a thread that + * will monitor for device connection/disconnection events. + * + ****************************************************************************/ + +int usbhost_waiter_initialize(FAR struct usbhost_connection_s *conn) +{ + FAR char *argv[2]; + char arg1[32]; + + /* Start a thread to handle device connection. */ + + snprintf(arg1, 16, "%p", conn); + argv[0] = arg1; + argv[1] = NULL; + return kthread_create("usbhost", + CONFIG_USBHOST_WAITER_PRIO, + CONFIG_USBHOST_WAITER_STACKSIZE, + usbhost_waiter, argv); +} diff --git a/include/nuttx/usb/usbhost.h b/include/nuttx/usb/usbhost.h index af67ca48a4..47e585a175 100644 --- a/include/nuttx/usb/usbhost.h +++ b/include/nuttx/usb/usbhost.h @@ -1303,6 +1303,29 @@ int usbhost_wlaninit(void); int usbhost_enumerate(FAR struct usbhost_hubport_s *hub, FAR struct usbhost_class_s **devclass); +#ifdef CONFIG_USBHOST_WAITER +/**************************************************************************** + * Name: usbhost_waiter_initialize + * + * Description: + * Initialize the USB host waiter. This function will start a thread that + * will monitor for device connection/disconnection events. + * + ****************************************************************************/ + +int usbhost_waiter_initialize(FAR struct usbhost_connection_s *conn); + +/**************************************************************************** + * Name: usbhost_drivers_initialize + * + * Description: + * Initialize all enabled USB host device drivers. + * + ****************************************************************************/ + +void usbhost_drivers_initialize(void); +#endif + #undef EXTERN #if defined(__cplusplus) }