Files
CherryUSB/demo/cdc_rndis_template.c
2023-06-21 19:39:22 +08:00

183 lines
5.2 KiB
C

#include "usbd_core.h"
#include "usbd_rndis.h"
/*!< endpoint address */
#define CDC_IN_EP 0x81
#define CDC_OUT_EP 0x02
#define CDC_INT_EP 0x83
#define USBD_VID 0xEFFF
#define USBD_PID 0xEFFF
#define USBD_MAX_POWER 100
#define USBD_LANGID_STRING 1033
/*!< config descriptor size */
#define USB_CONFIG_SIZE (9 + CDC_RNDIS_DESCRIPTOR_LEN)
#ifdef CONFIG_USB_HS
#define CDC_MAX_MPS 512
#else
#define CDC_MAX_MPS 64
#endif
/*!< global descriptor */
static const uint8_t cdc_descriptor[] = {
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
CDC_RNDIS_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
///////////////////////////////////////
/// string0 descriptor
///////////////////////////////////////
USB_LANGID_INIT(USBD_LANGID_STRING),
///////////////////////////////////////
/// string1 descriptor
///////////////////////////////////////
0x14, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */
'h', 0x00, /* wcChar1 */
'e', 0x00, /* wcChar2 */
'r', 0x00, /* wcChar3 */
'r', 0x00, /* wcChar4 */
'y', 0x00, /* wcChar5 */
'U', 0x00, /* wcChar6 */
'S', 0x00, /* wcChar7 */
'B', 0x00, /* wcChar8 */
///////////////////////////////////////
/// string2 descriptor
///////////////////////////////////////
0x2A, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */
'h', 0x00, /* wcChar1 */
'e', 0x00, /* wcChar2 */
'r', 0x00, /* wcChar3 */
'r', 0x00, /* wcChar4 */
'y', 0x00, /* wcChar5 */
'U', 0x00, /* wcChar6 */
'S', 0x00, /* wcChar7 */
'B', 0x00, /* wcChar8 */
' ', 0x00, /* wcChar9 */
'R', 0x00, /* wcChar10 */
'N', 0x00, /* wcChar11 */
'D', 0x00, /* wcChar12 */
'I', 0x00, /* wcChar13 */
'S', 0x00, /* wcChar14 */
' ', 0x00, /* wcChar15 */
'D', 0x00, /* wcChar16 */
'E', 0x00, /* wcChar17 */
'M', 0x00, /* wcChar18 */
'O', 0x00, /* wcChar19 */
///////////////////////////////////////
/// string3 descriptor
///////////////////////////////////////
0x16, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'2', 0x00, /* wcChar0 */
'0', 0x00, /* wcChar1 */
'2', 0x00, /* wcChar2 */
'2', 0x00, /* wcChar3 */
'1', 0x00, /* wcChar4 */
'2', 0x00, /* wcChar5 */
'3', 0x00, /* wcChar6 */
'4', 0x00, /* wcChar7 */
'5', 0x00, /* wcChar8 */
'6', 0x00, /* wcChar9 */
#ifdef CONFIG_USB_HS
///////////////////////////////////////
/// device qualifier descriptor
///////////////////////////////////////
0x0a,
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
0x00,
0x02,
0x02,
0x02,
0x01,
0x40,
0x01,
0x00,
#endif
0x00
};
#ifdef RT_USING_LWIP
#include <rtthread.h>
#include <rtdevice.h>
#include <netif/ethernetif.h>
struct eth_device rndis_dev;
void usbd_configure_done_callback(void)
{
eth_device_linkchange(&rndis_dev, RT_TRUE);
}
uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
{
switch (cmd) {
case NIOCTL_GADDR:
/* get mac address */
if (args)
rt_memcpy(args, mac, 6);
else
return -RT_ERROR;
break;
default:
break;
}
return RT_EOK;
}
struct pbuf *rt_usbd_rndis_eth_rx(rt_device_t dev)
{
return usbd_rndis_eth_rx();
}
rt_err_t rt_usbd_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
{
return usbd_rndis_eth_tx(p);
}
void usbd_rndis_data_recv(uint8_t *data, uint32_t len)
{
eth_device_ready(&rndis_dev);
}
void rt_usbd_rndis_init(void)
{
rndis_dev.parent.control = rt_usbd_rndis_control;
rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
rndis_dev.eth_tx = rt_usbd_rndis_eth_tx;
eth_device_init(&rndis_dev, "u0");
eth_device_linkchange(&rndis_dev, RT_FALSE);
}
#else
void usbd_configure_done_callback(void)
{
}
#endif /* RT_USING_LWIP */
struct usbd_interface intf0;
struct usbd_interface intf1;
void cdc_rndis_init(void)
{
#ifdef RT_USING_LWIP
rt_usbd_rndis_init();
#endif
usbd_desc_register(cdc_descriptor);
usbd_add_interface(usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
usbd_add_interface(usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
usbd_initialize();
}