sim/usb: add sim usb device

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
This commit is contained in:
zhangyuan21
2022-12-01 09:54:11 +08:00
committed by Alan Carvalho de Assis
parent 699c930987
commit c61c694a77
14 changed files with 2700 additions and 0 deletions
+30
View File
@@ -605,4 +605,34 @@ config SIM_UART3_NAME
endmenu
config SIM_USB_DEV
bool "Linux USB Device"
select USBDEV
---help---
Build in support for simulated usb device
if SIM_USB_DEV
config SIM_USB_RAW_GADGET
bool "Simulated USB Raw Gadget Dev"
default n
depends on HOST_LINUX
---help---
Use USB Raw Gadget and Dummy HCD/UDC to set up virtual
USB Device and Host controller that connected to each
other inside the kernel.
Get Raw Gadget:
Get Raw Gadget code at https://github.com/xairy/raw-gadget.
Make Raw Gadget:
Run make in the raw_gadget and dummy_hcd directory. If raw_gadget
build fail, you need to check which register interface meets your
kenel version, usb_gadget_probe_driver or usb_gadget_register_driver.
Install Raw Gadget:
Run ./insmod.sh in the raw_gadget and dummy_hcd directory.
endif
endif # ARCH_SIM
+7
View File
@@ -198,6 +198,13 @@ ifeq ($(CONFIG_SIM_SPI_LINUX),y)
HOSTSRCS += sim_linuxspi.c
endif
ifeq ($(CONFIG_SIM_USB_DEV),y)
CSRCS += sim_usbdev.c
ifeq ($(CONFIG_SIM_USB_RAW_GADGET),y)
HOSTSRCS += sim_rawgadget.c
endif
endif
ifeq ($(CONFIG_RPTUN),y)
CSRCS += sim_rptun.c
endif
File diff suppressed because it is too large Load Diff
+8
View File
@@ -203,6 +203,10 @@ static int sim_loop_task(int argc, char **argv)
sim_video_loop();
#endif
#ifdef CONFIG_SIM_USB_DEV
sim_usbdev_loop();
#endif
#ifdef CONFIG_MOTOR_FOC_DUMMY
/* Update simulated FOC device */
@@ -289,6 +293,10 @@ void up_initialize(void)
audio_register("pcm1c", sim_audio_initialize(false, true));
#endif
#ifdef CONFIG_SIM_USB_DEV
sim_usbdev_initialize();
#endif
kthread_create("loop_task", SCHED_PRIORITY_MAX,
CONFIG_DEFAULT_TASK_STACKSIZE,
sim_loop_task, NULL);
+7
View File
@@ -383,6 +383,13 @@ int sim_video_initialize(void);
void sim_video_loop(void);
#endif
/* sim_usbdev.c *************************************************************/
#ifdef CONFIG_SIM_USB_DEV
void sim_usbdev_initialize(void);
int sim_usbdev_loop(void);
#endif
/* Debug ********************************************************************/
#ifdef CONFIG_STACK_COLORATION
File diff suppressed because it is too large Load Diff
+87
View File
@@ -0,0 +1,87 @@
/****************************************************************************
* arch/sim/src/sim/sim_usbdev.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_DEV_H
#define __ARCH_SIM_SRC_SIM_USB_DEV_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;
uint8_t data[0];
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Host USB Interface */
int host_usbdev_init(uint32_t speed);
int host_usbdev_deinit(void);
int host_usbdev_pullup(bool enable);
int host_usbdev_epconfig(uint8_t epno,
const struct host_usb_epdesc_s *epdesc);
int host_usbdev_epdisable(uint8_t epno);
int host_usbdev_epstall(uint8_t epno, bool resume);
int host_usbdev_epcancel(uint8_t epno);
int host_usbdev_epwrite(uint8_t epno, uint8_t flags,
uint8_t *data, uint16_t len);
struct host_usb_ctrlreq_s *host_usbdev_ep0read(void);
uint8_t *host_usbdev_epread(uint8_t epno, uint16_t *len);
void host_usbdev_epread_end(uint8_t epno);
#endif /* __ARCH_SIM_SRC_SIM_USB_DEV_H */
+9
View File
@@ -59,4 +59,13 @@ config SIM_WTGAHRS2_UARTN
We can select the number according to which SIM_UARTX_NAME is used to sensor.
This range is 0-4.
config SIM_RNDIS_MACADDR
hex "RNDIS MAC address"
default 0xfadedeadbeef
depends on RNDIS
---help---
If the hardware has no built-in MAC address then the fixed,
software-assigned MAC address MAC address must provided
with this selection.
endif
+114
View File
@@ -1435,3 +1435,117 @@ wamr
[0]crcfinal : 0xa14c
Correct operation validated. See README.md for run and reporting rules.
CoreMark 1.0 : 5.000000 / Clang 15.0.7 Using NuttX compilation options / Defined by the NuttX configuration
usbdev
This is a configuration with sim usbdev support.
1. Raw Gadget setup
Get Raw Gadget:
Get Raw Gadget code at https://github.com/xairy/raw-gadget.
Make Raw Gadget:
Run make in the raw_gadget and dummy_hcd directory. If raw_gadget build
fail, you need to check which register interface meets your kenel version,
usb_gadget_probe_driver or usb_gadget_register_driver.
Install Raw Gadget:
Run ./insmod.sh in the raw_gadget and dummy_hcd directory.
2. Configuration
sim:usbdev contains two different sets of composite devices:
conn0: adb & rndis
conn1: cdcacm & cdcecm
You can use the sim:usbdev configuration:
./tools/configure.sh sim:usbdev
3. How to run
Run nuttx with root mode, then you can use it as the following:
1> Run ADB:
NuttX enter command:
$ conn 0
$ adbd &
Host PC enter the ADB command:
$ adb kill-server
$ adb devices
List of devices attached
* daemon not running; starting now at tcp:5037
* daemon started successfully
0101 device
If ADB connection fails, make sure the udev rule is added correctly.
Edit /etc/udev/rules.d/51-android.rules file and add the following to it:
SUBSYSTEM=="usb", ATTR{idVendor}=="1630", ATTR{idProduct}=="0042", MODE="0666", GROUP="plugdev"
Then you can use commands such as adb shell, adb push, adb pull as normal.
2> Run RNDIS:
NuttX enter command:
$ conn 0
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00 at UP
inet addr:0.0.0.0 DRaddr:0.0.0.0 Mask:0.0.0.0
$ dhcpd_start eth0
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00 at UP
inet addr:10.0.0.1 DRaddr:10.0.0.1 Mask:255.255.255.0
Host PC, you can see the network device named usb0:
$ ifconfig
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 602
inet 10.0.0.4 netmask 255.255.255.0 broadcast 10.0.0.255
ether 36:50:3d:62:b5:80 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43 bytes 8544 (8.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Then you can test the network connection using the ping command or telnet.
3> Run CDCACM:
NuttX enter command:
$ conn 1
If the connection is successful, you can see /dev/ttyACM devices on both NuttX
and host PC.
Then you can use echo and cat command to test:
NuttX:
nsh> echo hello > /dev/ttyACM0
Host PC:
$ cat /dev/ttyACM0
hello
3> Run CDCECM:
NuttX enter command:
$ conn 1
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:e0:de:ad:be:ef at UP
inet addr:0.0.0.0 DRaddr:0.0.0.0 Mask:0.0.0.0
$ dhcpd_start eth0
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:e0:de:ad:be:ef at UP
inet addr:10.0.0.1 DRaddr:10.0.0.1 Mask:255.255.255.0
Host PC, you can see the network device named enx020000112233:
$ ifconfig
enx020000112233: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 576
inet 10.0.0.4 netmask 255.255.255.0 broadcast 10.0.0.255
ether 02:00:00:11:22:33 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9143 (9.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Then you can test the network connection using the ping command or telnet.
@@ -0,0 +1,82 @@
#
# 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_ADBD_FILE_SERVICE=y
CONFIG_ADBD_FILE_SYMLINK=y
CONFIG_ADBD_SHELL_SERVICE=y
CONFIG_ADBD_USB_SERVER=y
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_BOARD_LATE_INITIALIZE=y
CONFIG_BUILTIN=y
CONFIG_CDCACM=y
CONFIG_CDCACM_COMPOSITE=y
CONFIG_CDCECM_COMPOSITE=y
CONFIG_COMPOSITE_IAD=y
CONFIG_COMPOSITE_PRODUCTID=0x0042
CONFIG_COMPOSITE_VENDORID=0x1630
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_EXAMPLES_DHCPD=y
CONFIG_FS_PROCFS=y
CONFIG_FS_TMPFS=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_LIBC_DLFCN=y
CONFIG_LIBUV=y
CONFIG_NETUTILS_DHCPD=y
CONFIG_NETUTILS_TELNETC=y
CONFIG_NETUTILS_TELNETD=y
CONFIG_NET_BROADCAST=y
CONFIG_NET_CDCECM=y
CONFIG_NET_ICMP=y
CONFIG_NET_ICMP_SOCKET=y
CONFIG_NET_LL_GUARDSIZE=50
CONFIG_NET_SOCKOPTS=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_READLINE=y
CONFIG_PSEUDOFS_SOFTLINKS=y
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_READLINE_TABCOMPLETION=y
CONFIG_RNDIS=y
CONFIG_RNDIS_COMPOSITE=y
CONFIG_SCHED_CHILD_STATUS=y
CONFIG_SCHED_HAVE_PARENT=y
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SIM_USB_DEV=y
CONFIG_SIM_USB_RAW_GADGET=y
CONFIG_SYSLOG_CHARDEV=y
CONFIG_SYSLOG_MAX_CHANNELS=2
CONFIG_SYSTEM_ADBD=y
CONFIG_SYSTEM_CLE=y
CONFIG_SYSTEM_COMPOSITE=y
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_PING=y
CONFIG_TESTING_OSTEST=y
CONFIG_TLS_TASK_NELEM=4
CONFIG_USBADB=y
CONFIG_USBADB_COMPOSITE=y
CONFIG_USBDEV_COMPOSITE=y
CONFIG_USBDEV_DUALSPEED=y
+4
View File
@@ -67,4 +67,8 @@ ifeq ($(CONFIG_MOTOR_FOC_DUMMY),y)
CSRCS += sim_foc.c
endif
ifeq ($(CONFIG_USBDEV_COMPOSITE),y)
CSRCS += sim_composite.c
endif
include $(TOPDIR)/boards/Board.mk
+19
View File
@@ -53,6 +53,8 @@
#include <nuttx/wireless/bluetooth/bt_null.h>
#include <nuttx/wireless/bluetooth/bt_uart_shim.h>
#include <nuttx/wireless/ieee802154/ieee802154_loopback.h>
#include <nuttx/usb/adb.h>
#include <nuttx/usb/rndis.h>
#ifdef CONFIG_LCD_DEV
#include <nuttx/lcd/lcd_dev.h>
@@ -496,5 +498,22 @@ int sim_bringup(void)
rc_dummy_initialize(0);
#endif
#if defined(CONFIG_USBADB) && !defined(CONFIG_USBADB_COMPOSITE)
usbdev_adb_initialize();
#endif
#if defined(CONFIG_RNDIS) && !defined(CONFIG_RNDIS_COMPOSITE)
/* Set up a MAC address for the RNDIS device. */
uint8_t mac[6];
mac[0] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 5)) & 0xff;
mac[1] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 4)) & 0xff;
mac[2] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 3)) & 0xff;
mac[3] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 2)) & 0xff;
mac[4] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 1)) & 0xff;
mac[5] = (CONFIG_SIM_RNDIS_MACADDR >> (8 * 0)) & 0xff;
usbdev_rndis_initialize(mac);
#endif
return ret;
}
+260
View File
@@ -0,0 +1,260 @@
/****************************************************************************
* boards/sim/sim/sim/src/sim_composite.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 <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/board.h>
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/adb.h>
#include <nuttx/usb/rndis.h>
#include <nuttx/usb/cdcacm.h>
#include <nuttx/usb/cdcecm.h>
#include <nuttx/usb/composite.h>
#if defined(CONFIG_BOARDCTL_USBDEVCTRL) && defined(CONFIG_USBDEV_COMPOSITE)
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: board_composite0_connect
*
* Description:
* Connect the USB composite device on the specified USB device port for
* configuration 0.
*
* Input Parameters:
* port - The USB device port.
*
* Returned Value:
* A non-NULL handle value is returned on success. NULL is returned on
* any failure.
*
****************************************************************************/
static void *board_composite0_connect(int port)
{
struct composite_devdesc_s dev[2];
int ifnobase = 0;
int strbase = COMPOSITE_NSTRIDS - 1;
int dev_idx = 0;
#ifdef CONFIG_RNDIS
/* Configure the RNDIS USB device */
usbdev_rndis_get_composite_devdesc(&dev[dev_idx]);
/* Interfaces */
dev[dev_idx].devinfo.ifnobase = ifnobase;
dev[dev_idx].minor = 0;
/* Strings */
dev[dev_idx].devinfo.strbase = strbase;
/* Endpoints */
dev[dev_idx].devinfo.epno[RNDIS_EP_BULKIN_IDX] = 1;
dev[dev_idx].devinfo.epno[RNDIS_EP_BULKOUT_IDX] = 2;
dev[dev_idx].devinfo.epno[RNDIS_EP_INTIN_IDX] = 5;
/* Count up the base numbers */
ifnobase += dev[dev_idx].devinfo.ninterfaces;
strbase += dev[dev_idx].devinfo.nstrings;
dev_idx += 1;
#endif
#ifdef CONFIG_USBADB
/* Configure the ADB USB device */
usbdev_adb_get_composite_devdesc(&dev[dev_idx]);
/* Interfaces */
dev[dev_idx].devinfo.ifnobase = ifnobase;
dev[dev_idx].minor = 0;
/* Strings */
dev[dev_idx].devinfo.strbase = strbase;
/* Endpoints */
dev[dev_idx].devinfo.epno[USBADB_EP_BULKIN_IDX] = 6;
dev[dev_idx].devinfo.epno[USBADB_EP_BULKOUT_IDX] = 7;
/* Count up the base numbers */
ifnobase += dev[dev_idx].devinfo.ninterfaces;
strbase += dev[dev_idx].devinfo.nstrings;
dev_idx += 1;
#endif
return composite_initialize(dev_idx, dev);
}
/****************************************************************************
* Name: board_composite1_connect
*
* Description:
* Connect the USB composite device on the specified USB device port for
* configuration 1.
*
* Input Parameters:
* port - The USB device port.
*
* Returned Value:
* A non-NULL handle value is returned on success. NULL is returned on
* any failure.
*
****************************************************************************/
static void *board_composite1_connect(int port)
{
struct composite_devdesc_s dev[2];
int ifnobase = 0;
int strbase = COMPOSITE_NSTRIDS - 1;
int dev_idx = 0;
#ifdef CONFIG_CDCACM
/* Configure the CDC/ACM device */
cdcacm_get_composite_devdesc(&dev[dev_idx]);
/* The callback functions for the CDC/ACM class */
dev[dev_idx].classobject = cdcacm_classobject;
dev[dev_idx].uninitialize = cdcacm_uninitialize;
/* Interfaces */
dev[dev_idx].devinfo.ifnobase = ifnobase;
dev[dev_idx].minor = 0;
/* Strings */
dev[dev_idx].devinfo.strbase = strbase;
/* Endpoints */
dev[dev_idx].devinfo.epno[CDCACM_EP_INTIN_IDX] = 5;
dev[dev_idx].devinfo.epno[CDCACM_EP_BULKIN_IDX] = 6;
dev[dev_idx].devinfo.epno[CDCACM_EP_BULKOUT_IDX] = 7;
/* Count up the base numbers */
ifnobase += dev[dev_idx].devinfo.ninterfaces;
strbase += dev[dev_idx].devinfo.nstrings;
dev_idx += 1;
#endif
#ifdef CONFIG_NET_CDCECM
/* Configure the CDC/ECM device */
cdcecm_get_composite_devdesc(&dev[dev_idx]);
/* Interfaces */
dev[dev_idx].devinfo.ifnobase = ifnobase;
dev[dev_idx].minor = 0;
/* Strings */
dev[dev_idx].devinfo.strbase = strbase;
/* Endpoints */
dev[dev_idx].devinfo.epno[CDCECM_EP_INTIN_IDX] = 10;
dev[dev_idx].devinfo.epno[CDCECM_EP_BULKIN_IDX] = 11;
dev[dev_idx].devinfo.epno[CDCECM_EP_BULKOUT_IDX] = 12;
/* Count up the base numbers */
ifnobase += dev[dev_idx].devinfo.ninterfaces;
strbase += dev[dev_idx].devinfo.nstrings;
dev_idx += 1;
#endif
return composite_initialize(dev_idx, dev);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_composite_initialize
*
* Description:
* Perform architecture specific initialization of a composite USB device.
*
****************************************************************************/
int board_composite_initialize(int port)
{
return OK;
}
/****************************************************************************
* Name: board_composite_connect
*
* Description:
* Connect the USB composite device on the specified USB device port using
* the specified configuration. The interpretation of the configid is
* board specific.
*
* Input Parameters:
* port - The USB device port.
* configid - The USB composite configuration
*
* Returned Value:
* A non-NULL handle value is returned on success. NULL is returned on
* any failure.
*
****************************************************************************/
void *board_composite_connect(int port, int configid)
{
if (configid == 0)
{
return board_composite0_connect(port);
}
else
{
return board_composite1_connect(port);
}
}
#endif /* CONFIG_BOARDCTL_USBDEVCTRL && CONFIG_USBDEV_COMPOSITE */
+16
View File
@@ -505,6 +505,22 @@ static const char *g_white_list[] =
"NimMain",
/* Ref:
* sim/posix/sim_rawgadget.c
*/
"bRequestType",
"bRequest",
"wValue",
"wIndex",
"wLength",
"bLength",
"bDescriptorType",
"bEndpointAddress",
"bmAttributes",
"wMaxPacketSize",
"bInterval",
NULL
};