sim/usb: add sim usb host

signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
This commit is contained in:
zhangyuan21
2022-11-30 19:08:14 +08:00
committed by Alan Carvalho de Assis
parent c61c694a77
commit 0af63cfc48
10 changed files with 1746 additions and 0 deletions
+35
View File
@@ -635,4 +635,39 @@ config SIM_USB_RAW_GADGET
endif
config SIM_USB_HOST
bool "Linux USB Host"
select USBHOST
select USBHOST_HAVE_ASYNCH
select USBHOST_ASYNCH
---help---
Build in support for simulated usb host
if SIM_USB_HOST
config SIM_LIBUSB
bool "Simulated USB Host use libusb"
default n
depends on HOST_LINUX
---help---
Use libusb to set up virtual USB Host controller.
config SIM_USB_VID
hex "Simulated USB Dev VID"
default 0x18d1
config SIM_USB_PID
hex "Simulated USB Dev PID"
default 0x4e11
config SIM_USB_STACKSIZE
int "Simulated USB waiter stack size"
default 1024
config SIM_USB_PRIO
int "Simulated USB waiter task priority"
default 100
endif
endif # ARCH_SIM
+8
View File
@@ -205,6 +205,14 @@ ifeq ($(CONFIG_SIM_USB_RAW_GADGET),y)
endif
endif
ifeq ($(CONFIG_SIM_USB_HOST),y)
CSRCS += sim_usbhost.c
ifeq ($(CONFIG_SIM_LIBUSB),y)
HOSTSRCS += sim_libusb.c
STDLIBS += -lusb-1.0
endif
endif
ifeq ($(CONFIG_RPTUN),y)
CSRCS += sim_rptun.c
endif
File diff suppressed because it is too large Load Diff
+8
View File
@@ -216,6 +216,10 @@ static int sim_loop_task(int argc, char **argv)
sched_unlock();
up_irq_restore(flags);
#ifdef CONFIG_SIM_USB_HOST
sim_usbhost_loop();
#endif
/* Sleep minimal time, let the idle run */
usleep(CONFIG_SIM_LOOPTASK_INTERVAL);
@@ -297,6 +301,10 @@ void up_initialize(void)
sim_usbdev_initialize();
#endif
#ifdef CONFIG_SIM_USB_HOST
sim_usbhost_initialize();
#endif
kthread_create("loop_task", SCHED_PRIORITY_MAX,
CONFIG_DEFAULT_TASK_STACKSIZE,
sim_loop_task, NULL);
+7
View File
@@ -390,6 +390,13 @@ void sim_usbdev_initialize(void);
int sim_usbdev_loop(void);
#endif
/* sim_usbhost.c ************************************************************/
#ifdef CONFIG_SIM_USB_HOST
int sim_usbhost_initialize(void);
int sim_usbhost_loop(void);
#endif
/* Debug ********************************************************************/
#ifdef CONFIG_STACK_COLORATION
File diff suppressed because it is too large Load Diff
+93
View File
@@ -0,0 +1,93 @@
/****************************************************************************
* arch/sim/src/sim/sim_usbhost.h
*
* 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.
*
****************************************************************************/
#ifndef __ARCH_SIM_SRC_SIM_USB_HOST_H
#define __ARCH_SIM_SRC_SIM_USB_HOST_H
/****************************************************************************
* Included Files
****************************************************************************/
#ifdef __SIM__
#include "config.h"
#endif
#include <stdint.h>
#include <stdbool.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
/* NuttX Endpoint descriptor */
struct host_usb_epdesc_s
{
uint8_t len; /* Descriptor length */
uint8_t type; /* Descriptor type */
uint8_t addr; /* Endpoint address */
uint8_t attr; /* Endpoint attributes */
uint16_t mxpacketsize; /* Maximum packet size */
uint8_t interval; /* Interval */
};
/* This structure is used to send control requests to a USB device. */
struct host_usb_ctrlreq_s
{
uint8_t type; /* Matches request type */
uint8_t req; /* Matches request field */
uint16_t value;
uint16_t index;
uint16_t len;
};
struct host_usb_datareq_s
{
struct host_usb_datareq_s *flink;
uint8_t addr;
uint8_t xfrtype;
uint8_t *data;
uint16_t len;
uint16_t xfer;
bool success;
void *priv;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Host USB host Interface */
int host_usbhost_init(void);
bool host_usbhost_getconnstate(void);
int host_usbhost_open(void);
void host_usbhost_close(void);
int host_usbhost_ep0trans(struct host_usb_ctrlreq_s *ctrlreq,
struct host_usb_datareq_s *datareq);
int host_usbhost_eptrans(struct host_usb_datareq_s *datareq);
struct host_usb_datareq_s *host_usbhost_getcomplete(void);
#endif /* __ARCH_SIM_SRC_SIM_USB_HOST_H */
+26
View File
@@ -1549,3 +1549,29 @@ usbdev
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Then you can test the network connection using the ping command or telnet.
usbhost
This is a configuration with sim usbhost support.
1. Libusb1.0 setup
$ sudo apt-get -y install libusb-1.0-0-dev
$ sudo apt-get -y install libusb-1.0-0-dev:i386
2. Configuration
sim:usbhost support cdcacm.
You can use the sim:usbdev configuration:
$ ./tools/configure.sh sim:usbhost
Configure the device you want to connet:
CONFIG_SIM_USB_PID=0x0042
CONFIG_SIM_USB_VID=0x1630
3. How to run
Run sim usbhost with root mode, run sim usbdev or plug-in cdcacm usb device.
Then you can use /dev/ttyACM to transfer data.
@@ -0,0 +1,57 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_SIM_UART_DMA is not set
CONFIG_ARCH="sim"
CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BUILTIN=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_SCHED=y
CONFIG_DEBUG_SCHED_ERROR=y
CONFIG_DEBUG_SCHED_WARN=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEBUG_USB=y
CONFIG_DEBUG_USB_ERROR=y
CONFIG_DEBUG_USB_INFO=y
CONFIG_DEBUG_USB_WARN=y
CONFIG_DEBUG_WARN=y
CONFIG_FS_PROCFS=y
CONFIG_FS_TMPFS=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_LIBC_DLFCN=y
CONFIG_LIBC_EXECFUNCS=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_READLINE=y
CONFIG_PSEUDOFS_SOFTLINKS=y
CONFIG_PSEUDOTERM=y
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_READLINE_TABCOMPLETION=y
CONFIG_SCHED_CHILD_STATUS=y
CONFIG_SCHED_HAVE_PARENT=y
CONFIG_SCHED_LPNTHREADS=2
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SIM_LIBUSB=y
CONFIG_SIM_USB_HOST=y
CONFIG_SIM_USB_PID=0x0042
CONFIG_SIM_USB_STACKSIZE=2048
CONFIG_SIM_USB_VID=0x1630
CONFIG_SYSLOG_CHARDEV=y
CONFIG_SYSLOG_MAX_CHANNELS=2
CONFIG_SYSTEM_CLE=y
CONFIG_SYSTEM_NSH=y
CONFIG_TESTING_OSTEST=y
CONFIG_TLS_TASK_NELEM=4
CONFIG_USBHOST_CDCACM=y
CONFIG_USBHOST_COMPOSITE=y
+9
View File
@@ -521,6 +521,15 @@ static const char *g_white_list[] =
"wMaxPacketSize",
"bInterval",
/* Ref:
* sim/posix/sim_libusb.c
*/
"bNumConfigurations",
"bDeviceClass",
"idVendor",
"idProduct",
NULL
};