⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_ch9.c

📁 基于TI DSP/BIOS的usb网卡的实现
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "usb_ch9.h"
#include "usb_cdc.h"
#include "Hal4D13.h"
#include "rndisdev.h"

extern int rndis_dev_index;

/*string descriptor index*/
#define STRING_LANGUAGE_ID      0   //string descriptor index
#define STRING_MANUFACTURER     1   //string descriptor index
#define STRING_PRODUCT          2   //string descriptor index

/*CDC definition*/
#define USB_CDC_HEADER_TYPE             0x00        /* header_desc */
#define USB_CDC_CALL_MANAGEMENT_TYPE    0x01        /* call_mgmt_descriptor */
#define USB_CDC_UNION_TYPE              0x06        /* union_desc */
#define USB_CDC_SUBCLASS_ACM            0x02        /*cdc attribute*/
#define USB_CDC_ACM_PROTO_VENDOR        0xff        /*cdc attribute*/

/*total descriptor size 82 bytes*/

const struct usb_device_descriptor  device_descriptor = 
{
    USB_DT_DEVICE_SIZE,//sizeof( device_descriptor ),
    USB_DT_DEVICE,
    constant_cpu_to_le16 ( DEV_USB_VER ),
    USB_CLASS_COMM,
    0x00,
    0x00,
    0x40,
    constant_cpu_to_le16 ( VENDOR_NUM ),
    constant_cpu_to_le16 ( PRODUCT_NUM ),
    0x0100,
    STRING_MANUFACTURER,
    STRING_PRODUCT,
    0x00,
    0x01
};/*18 bytes*/

const struct usb_config_descriptor  config_descriptor = 
{
    USB_DT_CONFIG_SIZE,//sizeof( config_descriptor ),
    USB_DT_CONFIG,
    0x43, //calculated
    0x02,
    DEV_CONFIG_VALUE,
    0,
    USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
    0x32
};/*9 bytes*/

const struct usb_interface_descriptor class_interface_descriptor = 
{
    USB_DT_INTERFACE_SIZE,//sizeof( class_interface_descriptor ),
    USB_DT_INTERFACE,
    0x00,
    0x00,
    0x01,
    USB_CLASS_COMM,
    USB_CDC_SUBCLASS_ACM,
    USB_CDC_ACM_PROTO_VENDOR,
    0x00
};/*9 bytes*/

const u8 cdc_header_descriptor[5] =  //cdc header descriptor len is 5
{
    0x05,                   //descriptor length
    USB_DT_CS_INTERFACE,
    USB_CDC_HEADER_TYPE,
    0x10,                   //version high byte
    0x01                    //version low byte
};

const struct usb_cdc_union_desc cdc_union_descriptor = 
{
    0x05,//sizeof( cdc_union_descriptor ),
    USB_DT_CS_INTERFACE,
    USB_CDC_UNION_TYPE,
    0x00,
    0x01
};/*5 bytes*/

const struct usb_cdc_call_mgmt_descriptor cdc_call_mgmt_descriptor = 
{ 
    0x05,//sizeof( cdc_call_mgmt_descriptor ),
    USB_DT_CS_INTERFACE,
    USB_CDC_CALL_MANAGEMENT_TYPE,
    0x00,
    0x01
};/*5 bytes*/

const struct usb_cdc_acm_descriptor cdc_acm_descriptor = 
{
    0x04,//sizeof( cdc_acm_descriptor ),
    USB_DT_CS_INTERFACE,
    USB_CDC_ACM_TYPE,
    0x00
};/*4 bytes*/

//notification endpoint
const struct usb_endpoint_descriptor  ep_status_descriptor = 
{
    USB_DT_ENDPOINT_SIZE,
    USB_DT_ENDPOINT,
    USB_DIR_IN | EP_ID_INTERRUPT,
    USB_ENDPOINT_XFER_INT,
    constant_cpu_to_le16 ( STATUS_PACKET_SIZE ),
    0x1
};/*7 bytes*/

/*data channel interface*/
const struct usb_interface_descriptor  data_interface_descriptor = 
{
    USB_DT_INTERFACE_SIZE,//sizeof( data_interface_descriptor ),
    USB_DT_INTERFACE,
    0x01,
    0x00,
    0x02,
    USB_CLASS_CDC_DATA,
    0x00,
    0x00,
    0
};/*9 bytes*/

//data in endpoint
const struct usb_endpoint_descriptor ep_in_descriptor = 
{
    USB_DT_ENDPOINT_SIZE,
    USB_DT_ENDPOINT,
    USB_DIR_IN | EP_ID_BULK_IN,
    USB_ENDPOINT_XFER_BULK,
    constant_cpu_to_le16( BULK_PACKET_SIZE ),
    0x00
};/*7 bytes*/

//data out endpoint
const struct usb_endpoint_descriptor ep_out_descriptor = 
{
    USB_DT_ENDPOINT_SIZE,
    USB_DT_ENDPOINT,
    USB_DIR_OUT | EP_ID_BULK_OUT,
    USB_ENDPOINT_XFER_BULK,
    constant_cpu_to_le16( BULK_PACKET_SIZE ),
    0x00
};/*7 bytes*/

#define  STRING_MANUFACTURER_SIZE 4 /*XJGC*/
#define  STRING_PRODUCT_SIZE      6 /*DMU800*/
#define  LANGUAGE_ID              0x0409 /*en-us*/

const u8 str_languageid_descriptor[4] = 
{
    0x04,                          //descripotr length
    USB_DT_STRING,
    ( u8 )LANGUAGE_ID,
    ( u8 )( LANGUAGE_ID >> 8 )    //language_id high byte
};

const u8 str_manufacturer_descriptor[STRING_MANUFACTURER_SIZE * 2 + 2] = //unicode string
{
    sizeof( str_manufacturer_descriptor ),
    USB_DT_STRING,
    'X', 0, 'J', 0, 'G', 0, 'C', 0
};/*10 bytes*/

const u8 str_product_descriptor[STRING_PRODUCT_SIZE * 2 + 2] = //unicode string
{
    sizeof( str_product_descriptor ),
    USB_DT_STRING,
    'D', 0, 'M', 0, 'U', 0, '8', 0, '0', 0, '0', 0
};/*14 bytes*/

static const struct usb_descriptor_header * hs_rndis_function [] = {
    ( struct usb_descriptor_header *)& device_descriptor,
        ( struct usb_descriptor_header *)& config_descriptor,
        ( struct usb_descriptor_header *)& class_interface_descriptor,
        ( struct usb_descriptor_header *)& cdc_header_descriptor,        
        ( struct usb_descriptor_header *)& cdc_call_mgmt_descriptor,
        ( struct usb_descriptor_header *)& cdc_acm_descriptor,
        ( struct usb_descriptor_header *)& cdc_union_descriptor,
        ( struct usb_descriptor_header *)& ep_status_descriptor,
        ( struct usb_descriptor_header *)& data_interface_descriptor,
        ( struct usb_descriptor_header *)& ep_in_descriptor,
        ( struct usb_descriptor_header *)& ep_out_descriptor,
        NULL
};

//for get device descriptor use
static u16 usb_config_buf( u8 * buf, u16 buflen, const struct usb_descriptor_header  * * function )
{
    u16 len, i;
    const struct usb_descriptor_header * header;

    len = 0;
    for( i = 1; (*( function + i )) != NULL; i ++)
    {
        header = * ( function + i );
        if( ( len + header->bLength ) < buflen )
        {
            memcpy( buf + len, header, header->bLength );
            len += header->bLength;
        }
        else
        {
            memcpy( buf + len, header, buflen - len );
            len = buflen;
            break;
        }           
    }    
    return len;
}

static int  get_descriptor( u8 type, u8 index, u8 * buf, u16 size )
{
    int len = - ERR_NOTSUPP;
    switch( type )
    {
    case USB_DT_DEVICE:     
        len = sizeof( device_descriptor );
        if( len > ( int )size )
            len = size;     
        memcpy( buf, & device_descriptor, len );
        break;
    case USB_DT_CONFIG:
        len = usb_config_buf( buf, size,( const struct usb_descriptor_header  * *) & hs_rndis_function );
        break;
    case USB_DT_STRING:
        switch( index )
        {
        case STRING_LANGUAGE_ID:
            len = sizeof( str_languageid_descriptor );
            memcpy( buf,& str_languageid_descriptor, len );
            break;
        case STRING_MANUFACTURER:
            len = sizeof( str_manufacturer_descriptor );
            memcpy( buf,& str_manufacturer_descriptor, len );
            break;
        case STRING_PRODUCT:
            len = sizeof( str_product_descriptor );
            memcpy( buf,& str_product_descriptor, len );
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
    return len; 
}

static int get_status( u8 recipient, u16 epindex, u8 * buf )
{
    int len = - ERR_NOTSUPP;
    u8 tmp;
    switch( recipient )
    {
    case USB_RECIP_DEVICE:
        len = 2;//response len
        * buf = DEVSTS_SELFPOWERED; /*self powered,not support remote wakeup*/
        *( buf + 1 ) = 0x00;         
        break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -