usbdev: Use composite devices as universal devices

The enumeration process for composite and non-composite devices
has a lot of duplicate code. Treating the code for composite
devices as universal enumeration code can reduce redundant code.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
This commit is contained in:
zhangyuan21
2023-07-05 09:23:26 +08:00
committed by archer
parent d55e0cce9c
commit fe6b0619f9
6 changed files with 103 additions and 105 deletions

View File

@@ -35,7 +35,7 @@ ifeq ($(CONFIG_USBMSC),y)
endif
ifeq ($(CONFIG_USBDEV_COMPOSITE),y)
CSRCS += composite.c composite_desc.c
CSRCS += composite_desc.c
endif
ifeq ($(CONFIG_USBDEV_TRACE_STRINGS),y)
@@ -58,7 +58,8 @@ ifeq ($(CONFIG_NET_CDCECM),y)
CSRCS += cdcecm.c
endif
CSRCS += usbdev_req.c usbdev_trace.c usbdev_trprintf.c
CSRCS += composite.c usbdev_req.c
CSRCS += usbdev_trace.c usbdev_trprintf.c
# Include USB device build support

View File

@@ -40,8 +40,6 @@
#include "composite.h"
#ifdef CONFIG_USBDEV_COMPOSITE
/****************************************************************************
* Private Types
****************************************************************************/
@@ -108,16 +106,6 @@ static const struct usbdevclass_driverops_s g_driverops =
composite_resume, /* resume */
};
/****************************************************************************
* Public Data
****************************************************************************/
const char g_compvendorstr[] = CONFIG_COMPOSITE_VENDORSTR;
const char g_compproductstr[] = CONFIG_COMPOSITE_PRODUCTSTR;
#ifndef CONFIG_COMPOSITE_BOARD_SERIALSTR
const char g_compserialstr[] = CONFIG_COMPOSITE_SERIALSTR;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -512,7 +500,7 @@ static int composite_setup(FAR struct usbdevclass_driver_s *driver,
{
#ifdef CONFIG_USBDEV_DUALSPEED
ret = composite_mkcfgdesc(priv, ctrlreq->buf, dev->speed,
ctrl->value[1]);
ctrl->value[1]);
#else
ret = composite_mkcfgdesc(priv, ctrlreq->buf);
#endif
@@ -527,6 +515,7 @@ static int composite_setup(FAR struct usbdevclass_driver_s *driver,
FAR struct usb_strdesc_s *buf =
(FAR struct usb_strdesc_s *)ctrlreq->buf;
#ifdef CONFIG_USBDEV_COMPOSITE
if (strid < COMPOSITE_NSTRIDS)
{
ret = composite_mkstrdesc(strid, buf);
@@ -571,6 +560,9 @@ static int composite_setup(FAR struct usbdevclass_driver_s *driver,
}
}
}
#else
ret = composite_mkstrdesc(strid, buf);
#endif
}
break;
@@ -587,7 +579,7 @@ static int composite_setup(FAR struct usbdevclass_driver_s *driver,
case USB_REQ_SETCONFIGURATION:
{
if (ctrl->type == 0)
if (ctrl->type == 0 && value != priv->config)
{
int i;
@@ -622,7 +614,7 @@ static int composite_setup(FAR struct usbdevclass_driver_s *driver,
case USB_REQ_SETINTERFACE:
{
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE &&
priv->config == COMPOSITE_CONFIGID)
priv->config != COMPOSITE_CONFIGIDNONE)
{
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
dispatched = true;
@@ -957,7 +949,7 @@ FAR void *composite_initialize(uint8_t ndevices,
if (ret < 0)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_DEVREGISTER),
(uint16_t)-ret);
(uint16_t)-ret);
goto errout_with_alloc;
}
@@ -1061,5 +1053,3 @@ int composite_ep0submit(FAR struct usbdevclass_driver_s *driver,
return 0;
}
}
#endif /* CONFIG_USBDEV_COMPOSITE */

View File

@@ -35,86 +35,21 @@
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/usbdev_trace.h>
#ifdef CONFIG_USBDEV_COMPOSITE
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Packet sizes */
#ifndef CONFIG_COMPOSITE_EP0MAXPACKET
# define CONFIG_COMPOSITE_EP0MAXPACKET 64
#endif
/* Vendor and product IDs and strings */
#ifndef CONFIG_COMPOSITE_COMPOSITE
# ifndef CONFIG_COMPOSITE_VENDORID
# warning "CONFIG_COMPOSITE_VENDORID not defined"
# define CONFIG_COMPOSITE_VENDORID 0x03eb
# endif
# ifndef CONFIG_COMPOSITE_PRODUCTID
# warning "CONFIG_COMPOSITE_PRODUCTID not defined"
# define CONFIG_COMPOSITE_PRODUCTID 0x2022
# endif
# ifndef CONFIG_COMPOSITE_VERSIONNO
# define CONFIG_COMPOSITE_VERSIONNO (0x0101)
# endif
# ifndef CONFIG_COMPOSITE_VENDORSTR
# warning "No Vendor string specified"
# define CONFIG_COMPOSITE_VENDORSTR "NuttX"
# endif
# ifndef CONFIG_COMPOSITE_PRODUCTSTR
# warning "No Product string specified"
# define CONFIG_COMPOSITE_PRODUCTSTR "Composite Device"
# endif
# undef CONFIG_COMPOSITE_SERIALSTR
# define CONFIG_COMPOSITE_SERIALSTR "0101"
#endif
#undef CONFIG_COMPOSITE_CONFIGSTR
#define CONFIG_COMPOSITE_CONFIGSTR "Composite"
#ifdef CONFIG_USBDEV_SELFPOWERED
# define COMPOSITE_SELFPOWERED USB_CONFIG_ATTR_SELFPOWER
#ifdef CONFIG_USBDEV_COMPOSITE
# define NUM_DEVICES_TO_HANDLE (8)
#else
# define COMPOSITE_SELFPOWERED (0)
# define NUM_DEVICES_TO_HANDLE (1)
#endif
#ifdef CONFIG_USBDEV_REMOTEWAKEUP
# define COMPOSITE_REMOTEWAKEUP USB_CONFIG_ATTR_WAKEUP
#else
# define COMPOSITE_REMOTEWAKEUP (0)
#endif
#define NUM_DEVICES_TO_HANDLE (8)
/* Descriptors **************************************************************/
/* These settings are not modifiable via the NuttX configuration */
#define COMPOSITE_CONFIGIDNONE (0) /* Config ID = 0 means to return to address mode */
#define COMPOSITE_CONFIGID (1) /* The only supported configuration ID */
/* String language */
#define COMPOSITE_STR_LANGUAGE (0x0409) /* en-us */
/* Descriptor strings */
#define COMPOSITE_MANUFACTURERSTRID (1)
#define COMPOSITE_PRODUCTSTRID (2)
#define COMPOSITE_SERIALSTRID (3)
#define COMPOSITE_CONFIGSTRID (4)
/****************************************************************************
* Public Types
****************************************************************************/
@@ -136,23 +71,24 @@ struct composite_devobj_s
struct composite_dev_s
{
FAR struct usbdev_s *usbdev; /* usbdev driver pointer */
uint8_t config; /* Configuration number */
FAR struct usbdev_req_s *ctrlreq; /* Allocated control request */
uint8_t ndevices; /* Num devices in this composite device */
int cfgdescsize; /* Total size of the configuration descriptor: */
int ninterfaces; /* The total number of interfaces in this composite device */
struct composite_devobj_s device[NUM_DEVICES_TO_HANDLE]; /* Device class object */
FAR struct usbdev_s *usbdev; /* usbdev driver pointer */
FAR struct usbdev_req_s *ctrlreq; /* Allocated control request */
int cfgdescsize; /* Total size of the configuration descriptor: */
int ninterfaces; /* The total number of interfaces in this composite device */
uint8_t config; /* Configuration number */
uint8_t ndevices; /* Num devices in this composite device */
struct composite_devobj_s device[NUM_DEVICES_TO_HANDLE]; /* Device class object */
};
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef CONFIG_USBDEV_COMPOSITE
extern const char g_compvendorstr[];
extern const char g_compproductstr[];
extern const char g_compserialstr[];
#endif
/****************************************************************************
* Public Function Prototypes
@@ -176,9 +112,7 @@ int composite_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc);
*
****************************************************************************/
#ifndef CONFIG_COMPOSITE_COMPOSITE
FAR const struct usb_devdesc_s *composite_getdevdesc(void);
#endif
/****************************************************************************
* Name: composite_mkcfgdesc
@@ -208,5 +142,4 @@ int16_t composite_mkcfgdesc(FAR struct composite_dev_s *priv,
FAR const struct usb_qualdesc_s *composite_getqualdesc(void);
#endif
#endif /* CONFIG_USBDEV_COMPOSITE */
#endif /* __DRIVERS_USBDEV_COMPOSITE_H */

View File

@@ -42,6 +42,75 @@
#ifdef CONFIG_USBDEV_COMPOSITE
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Packet sizes */
#ifndef CONFIG_COMPOSITE_EP0MAXPACKET
# define CONFIG_COMPOSITE_EP0MAXPACKET 64
#endif
/* Vendor and product IDs and strings */
#ifndef CONFIG_COMPOSITE_VENDORID
# warning "CONFIG_COMPOSITE_VENDORID not defined"
# define CONFIG_COMPOSITE_VENDORID 0x03eb
#endif
#ifndef CONFIG_COMPOSITE_PRODUCTID
# warning "CONFIG_COMPOSITE_PRODUCTID not defined"
# define CONFIG_COMPOSITE_PRODUCTID 0x2022
#endif
#ifndef CONFIG_COMPOSITE_VERSIONNO
# define CONFIG_COMPOSITE_VERSIONNO (0x0101)
#endif
#ifndef CONFIG_COMPOSITE_VENDORSTR
# warning "No Vendor string specified"
# define CONFIG_COMPOSITE_VENDORSTR "NuttX"
#endif
#ifndef CONFIG_COMPOSITE_PRODUCTSTR
# warning "No Product string specified"
# define CONFIG_COMPOSITE_PRODUCTSTR "Composite Device"
#endif
#undef CONFIG_COMPOSITE_SERIALSTR
#define CONFIG_COMPOSITE_SERIALSTR "0101"
#undef CONFIG_COMPOSITE_CONFIGSTR
#define CONFIG_COMPOSITE_CONFIGSTR "Composite"
#ifdef CONFIG_USBDEV_SELFPOWERED
# define COMPOSITE_SELFPOWERED USB_CONFIG_ATTR_SELFPOWER
#else
# define COMPOSITE_SELFPOWERED (0)
#endif
#ifdef CONFIG_USBDEV_REMOTEWAKEUP
# define COMPOSITE_REMOTEWAKEUP USB_CONFIG_ATTR_WAKEUP
#else
# define COMPOSITE_REMOTEWAKEUP (0)
#endif
/* Descriptors **************************************************************/
/* String language */
#define COMPOSITE_STR_LANGUAGE (0x0409) /* en-us */
/* Descriptor strings */
#define COMPOSITE_MANUFACTURERSTRID (1)
#define COMPOSITE_PRODUCTSTRID (2)
#define COMPOSITE_SERIALSTRID (3)
#define COMPOSITE_CONFIGSTRID (4)
/****************************************************************************
* Private Data
****************************************************************************/
@@ -102,6 +171,16 @@ static const struct usb_qualdesc_s g_qualdesc =
};
#endif
/****************************************************************************
* Public Data
****************************************************************************/
const char g_compvendorstr[] = CONFIG_COMPOSITE_VENDORSTR;
const char g_compproductstr[] = CONFIG_COMPOSITE_PRODUCTSTR;
#ifndef CONFIG_COMPOSITE_BOARD_SERIALSTR
const char g_compserialstr[] = CONFIG_COMPOSITE_SERIALSTR;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/

View File

@@ -28,8 +28,6 @@
#include <nuttx/config.h>
#include <nuttx/usb/usbdev.h>
#ifdef CONFIG_USBDEV_COMPOSITE
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@@ -142,5 +140,4 @@ int composite_ep0submit(FAR struct usbdevclass_driver_s *driver,
}
#endif
#endif /* CONFIG_USBDEV_COMPOSITE */
#endif /* __INCLUDE_NUTTX_USB_COMPOSITE_H */

View File

@@ -182,7 +182,7 @@ struct usbdev_devinfo_s
int epno[5]; /* Array holding the endpoint configuration for this device */
};
#ifdef CONFIG_USBDEV_COMPOSITE
struct usbdevclass_driver_s;
struct composite_devdesc_s
{
#ifdef CONFIG_USBDEV_DUALSPEED
@@ -213,7 +213,6 @@ struct composite_devdesc_s
struct usbdev_devinfo_s devinfo;
};
#endif
/* struct usbdev_req_s - describes one i/o request */
@@ -315,7 +314,6 @@ struct usbdev_s
/* USB Device Class Implementations *****************************************/
struct usbdevclass_driver_s;
struct usbdevclass_driverops_s
{
CODE int (*bind)(FAR struct usbdevclass_driver_s *driver,