440 lines
14 KiB
C
440 lines
14 KiB
C
/********************************************************************************
|
||
* Copyright (c) 2012, Beijing Tongfang Microelectroics Co., Ltd.
|
||
* All rights reserved.
|
||
* Module: USB
|
||
* Author: Yang Song
|
||
* Version: V1.0
|
||
* History:
|
||
* 2012-09-24 Original version
|
||
********************************************************************************/
|
||
|
||
#include "global.h"
|
||
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbDeviceDescriptor
|
||
* Description: Device Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbHidKbdDeviceDescriptor[] = { //HID keyboard 设备
|
||
0x12, // length
|
||
0x01, // Fixed device
|
||
0x10,0x01, // USB 1.1
|
||
0x00,
|
||
0x00,
|
||
0x00,
|
||
EP0PKTSIZE, // Default max ep0 packet length
|
||
//0xE6,0x08, // VID
|
||
//0x37,0x34, // CCID PID
|
||
0xf1,0x26, // VID
|
||
0x01,0x88, // CCID3 PID
|
||
0x00,0x01, // Device release number
|
||
0x01, // Index of string descriptor describing manufacturer
|
||
0x02, // Index of string descriptor describing product
|
||
0x03, // Index of string descriptor describing the device’s serial number
|
||
0x01, // Number of possible configurations
|
||
};
|
||
|
||
const u8 usbHidPosDeviceDescriptor[] = { //HID POS 设备
|
||
0x12, // length
|
||
0x01, // Fixed device
|
||
0x10,0x01, // USB 1.1
|
||
0x00,
|
||
0x00,
|
||
0x00,
|
||
EP0PKTSIZE, // Default max ep0 packet length
|
||
//0xE6,0x08, // VID
|
||
//0x37,0x34, // CCID PID
|
||
0xf1,0x26, // VID
|
||
0x03,0x88, // CCID3 PID
|
||
0x00,0x01, // Device release number
|
||
0x01, // Index of string descriptor describing manufacturer
|
||
0x02, // Index of string descriptor describing product
|
||
0x03, // Index of string descriptor describing the device’s serial number
|
||
0x01, // Number of possible configurations
|
||
};
|
||
|
||
|
||
const u8 usbVspDeviceDescriptor[] = { //虚拟串口
|
||
0x12, // length
|
||
0x01, // Fixed device
|
||
0x10,0x01, // USB 1.1
|
||
0x02, // CDC 通信类设备
|
||
0x00,
|
||
0x00,
|
||
EP0PKTSIZE, // Default max ep0 packet length
|
||
//0xE6,0x08, // VID
|
||
//0x37,0x34, // CCID PID
|
||
0xf1,0x26, // VID?
|
||
0x02,0x88, // CCID3 PID?
|
||
0x00,0x01, // Device release number
|
||
0x01, // Index of string descriptor describing manufacturer
|
||
0x02, // Index of string descriptor describing product
|
||
0x03, // Index of string descriptor describing the device’s serial number
|
||
0x01, // Number of possible configurations
|
||
};
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbConfigDescriptor
|
||
* Description: Config Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbHidKbdConfigDescriptor[] = {
|
||
0x09, // length
|
||
0x02, // Fixed config
|
||
HIDKBD_CFG_LEN,0x00,
|
||
0x01, // Only support 2 interface by this configuratio
|
||
0x01, // Configuration Value is 1
|
||
0x00, // Index of string descriptor describing this configuration
|
||
0xe0, // support remote wake up
|
||
0x32, // 100mA
|
||
};
|
||
|
||
const u8 usbHidPosConfigDescriptor[] = {
|
||
0x09, // length
|
||
0x02, // Fixed config
|
||
HIDPOS_CFG_LEN,0x00,
|
||
0x01, // Only support 2 interface by this configuratio
|
||
0x01, // Configuration Value is 1
|
||
0x00, // Index of string descriptor describing this configuration
|
||
0xe0, // support remote wake up
|
||
0x32, // 100mA
|
||
};
|
||
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbManufacturerStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbManufacturerStringDescriptor[] = {
|
||
0x08, // bLength
|
||
0x03, // bDescriptorType
|
||
0x54,0x00, // T
|
||
0x4D,0x00, // M
|
||
0x43,0x00, // C
|
||
};
|
||
/***************************************************************************
|
||
* Const Name : usbProductStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
//const u8 usbProductStringDescriptor[] = {
|
||
//0x08,
|
||
//0x03,
|
||
//0x31,0x00, // 1
|
||
//0x32,0x00, // 2
|
||
//0x33,0x00, // 3
|
||
//};
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbVspProductStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbVspProductStringDescriptor[] = {
|
||
0x36,
|
||
0x03,
|
||
0x56, 0x00, //V
|
||
0x69, 0x00, //i
|
||
0x72, 0x00, //r
|
||
0x74, 0x00, //t
|
||
0x75, 0x00, //u
|
||
0x61, 0x00, //a
|
||
0x6C, 0x00, //l
|
||
0x20, 0x00, //
|
||
0x43, 0x00, //C
|
||
0x6F, 0x00, //o
|
||
0x6D, 0x00, //m
|
||
0x50, 0x00, //P
|
||
0x6F, 0x00, //o
|
||
0x72, 0x00, //r
|
||
0x74, 0x00, //t
|
||
0x20, 0x00, //
|
||
0x69, 0x00, //i
|
||
0x6E, 0x00, //n
|
||
0x20, 0x00, //
|
||
0x46, 0x00, //F
|
||
0x53, 0x00, //S
|
||
0x20, 0x00, //
|
||
0x4D, 0x00, //M
|
||
0x6F, 0x00, //o
|
||
0x64, 0x00, //d
|
||
0x65, 0x00, //e
|
||
};
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbHidPosProductStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbHidPosProductStringDescriptor[] = {
|
||
0x0E,
|
||
0x03,
|
||
0x48, 0x00, //H
|
||
0x49, 0x00, //I
|
||
0x44, 0x00, //D
|
||
0x50, 0x00, //P
|
||
0x4F, 0x00, //O
|
||
0x53, 0x00, //S
|
||
};
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbHidKbdProductStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
//const u8 usbHidKbdProductStringDescriptor[] = {
|
||
//0x18,
|
||
//0x03,
|
||
//0x48, 0x00, //H
|
||
//0x49, 0x00, //I
|
||
//0x44, 0x00, //D
|
||
//0x4B, 0x00, //K
|
||
//0x65, 0x00, //e
|
||
//0x79, 0x00, //y
|
||
//0x42, 0x00, //B
|
||
//0x6F, 0x00, //o
|
||
//0x61, 0x00, //a
|
||
//0x72, 0x00, //r
|
||
//0x64, 0x00 //d
|
||
//};
|
||
|
||
const u8 usbHidKbdProductStringDescriptor[] = {
|
||
0x18, //
|
||
0x03,
|
||
0x53,0x00, // S
|
||
0x51,0x00, // Q
|
||
0x30,0x00, // 0
|
||
0x31,0x00, // 1
|
||
0x2D,0x00, // -
|
||
0x50,0x00, // P
|
||
0x44,0x00, // D
|
||
0x44,0x00, // D
|
||
0x2D,0x00, // -
|
||
0x44,0x00, // D
|
||
0x31,0x00 // 1
|
||
};
|
||
|
||
/***************************************************************************
|
||
* Const Name : usbSerialNumberStringDescriptor
|
||
* Description: String Descriptor for get descriptor standard request
|
||
**************************************************************************/
|
||
const u8 usbSerialNumberStringDescriptor[] = { //todo
|
||
0x1E,
|
||
0x03,
|
||
0x31,0x00, // 1
|
||
0x32,0x00, // 2
|
||
0x33,0x00, // 3
|
||
0x34,0x00, // 4
|
||
0x35,0x00, // 5
|
||
0x36,0x00, // 6
|
||
0x37,0x00, // 7
|
||
0x38,0x00, // 8
|
||
0x39,0x00, // 9
|
||
0x30,0x00, // 0
|
||
0x61,0x00, // a
|
||
0x62,0x00, // b
|
||
0x63,0x00, // c
|
||
0x64,0x00, // d
|
||
};
|
||
|
||
/***************************************************************************
|
||
* Function: usbStdReqGetDeviceDescriptor
|
||
* Description: Deal with get device descriptor request
|
||
* Input: NULL
|
||
* Output: NULL
|
||
* Return: NULL
|
||
* Other: NULL
|
||
**************************************************************************/
|
||
void usbStdReqGetDeviceDescriptor(void)
|
||
{
|
||
u8 *usbDeviceDescriptor;
|
||
|
||
gCommFlag = USB;
|
||
|
||
if(g_bUsbDevType == USBDEVTYPE_HIDPOS)
|
||
{
|
||
usbDeviceDescriptor = (u8 *)usbHidPosDeviceDescriptor;
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_HIDKBD)
|
||
{
|
||
usbDeviceDescriptor = (u8 *)usbHidKbdDeviceDescriptor;
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_VSP)
|
||
{
|
||
usbDeviceDescriptor = (u8 *)usbVspDeviceDescriptor;
|
||
}
|
||
|
||
Memcpy(USBbuf,(u8 *)usbDeviceDescriptor,usbDeviceDescriptor[0]);
|
||
|
||
// if((st_usbDeviceRequest.wLength == 0x40) || ((USB_Time == 0)&&(st_usbDeviceRequest.wLength > EP0PKTSIZE)))
|
||
// g_sUsbEp0TxLen = EP0PKTSIZE;
|
||
// else
|
||
if(st_usbDeviceRequest.wLength > usbDeviceDescriptor[0])
|
||
g_sUsbEp0TxLen = usbDeviceDescriptor[0];
|
||
else
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
|
||
g_sUsbEp0TxLen = g_sUsbEp0TxLen > EP0PKTSIZE ? EP0PKTSIZE : g_sUsbEp0TxLen;
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
// if(g_sUsbEp0TxLen == EP0PKTSIZE)
|
||
// {
|
||
// delay(0x2000);
|
||
// if(!EP0STSCXSTS)
|
||
// {
|
||
// usbEpnTxAll(0, USBbuf+EP0PKTSIZE, usbDeviceDescriptor[0]-EP0PKTSIZE);
|
||
// }
|
||
// }
|
||
}
|
||
|
||
/***************************************************************************
|
||
* Function: usbStdReqGetDeviceDescriptor
|
||
* Description: Deal with get config descriptor request
|
||
* Input: NULL
|
||
* Output: NULL
|
||
* Return: NULL
|
||
* Other: NULL
|
||
**************************************************************************/
|
||
void usbStdReqGetConfigDescriptor(void)
|
||
{
|
||
u16 copy_config_descriptor_len;
|
||
|
||
if(g_bUsbDevType == USBDEVTYPE_HIDPOS)
|
||
{
|
||
Memcpy(USBbuf,(u8 *)usbHidPosConfigDescriptor,usbHidPosConfigDescriptor[0]);
|
||
copy_config_descriptor_len = usbHidPosConfigDescriptor[0];
|
||
|
||
Memcpy(USBbuf + usbHidPosConfigDescriptor[0],(u8 *)usbHidPosInterfaceDescriptor,usbHidPosInterfaceDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidPosInterfaceDescriptor[0];
|
||
|
||
Memcpy(USBbuf + copy_config_descriptor_len,(u8 *)usbHidPosDeviceClassDescriptor,usbHidPosDeviceClassDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidPosDeviceClassDescriptor[0];
|
||
|
||
Memcpy(USBbuf + copy_config_descriptor_len,(u8 *)(usbHidPosEndpointDescriptor+1),usbHidPosEndpointDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidPosEndpointDescriptor[0];
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_HIDKBD)
|
||
{
|
||
Memcpy(USBbuf,(u8 *)usbHidKbdConfigDescriptor,usbHidKbdConfigDescriptor[0]);
|
||
copy_config_descriptor_len = usbHidKbdConfigDescriptor[0];
|
||
|
||
Memcpy(USBbuf + usbHidKbdConfigDescriptor[0],(u8 *)usbHidKbdInterfaceDescriptor,usbHidKbdInterfaceDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidKbdInterfaceDescriptor[0];
|
||
|
||
Memcpy(USBbuf + copy_config_descriptor_len,(u8 *)usbHidKbdDeviceClassDescriptor,usbHidKbdDeviceClassDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidKbdDeviceClassDescriptor[0];
|
||
|
||
Memcpy(USBbuf + copy_config_descriptor_len,(u8 *)(usbHidKbdEndpointDescriptor+1),usbHidKbdEndpointDescriptor[0]);
|
||
copy_config_descriptor_len += usbHidKbdEndpointDescriptor[0];
|
||
|
||
//增加轮询速度
|
||
Memcpy(USBbuf + copy_config_descriptor_len,(u8 *)(&gConfgBuf.HIDPollRate),1);
|
||
copy_config_descriptor_len += 1;
|
||
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_VSP)
|
||
{
|
||
Memcpy(USBbuf ,(u8 *)usbVspConfigDescriptor, VSP_CFG_LEN);
|
||
copy_config_descriptor_len = VSP_CFG_LEN;
|
||
//提前取出一部分参数
|
||
memcpy(g_bVspLineCoding, (u8*)vspLineCoding, 7);
|
||
}
|
||
|
||
if(st_usbDeviceRequest.wLength >= copy_config_descriptor_len)
|
||
g_sUsbEp0TxLen = copy_config_descriptor_len;
|
||
else
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
}
|
||
|
||
/***************************************************************************
|
||
* Function: usbStdReqGetDeviceDescriptor
|
||
* Description: Deal with get string descriptor request
|
||
* Input: NULL
|
||
* Output: NULL
|
||
* Return: NULL
|
||
* Other: NULL
|
||
**************************************************************************/
|
||
void usbStdReqGetStringDescriptor(void)
|
||
{
|
||
u8 * usbProductStringDescriptor;
|
||
if((st_usbDeviceRequest.wValue & 0x00FF) == 0x00) // For answering 1st Get_Descriport(String),Language ID,index= 0
|
||
{
|
||
USBbuf[0] = 0x04; // bLength
|
||
USBbuf[1] = 0x03; // bDescriptorType
|
||
USBbuf[2] = 0x09; // wLANGID = 0409,English(US)
|
||
USBbuf[3] = 0x04;
|
||
|
||
if(st_usbDeviceRequest.wLength >= 4)
|
||
g_sUsbEp0TxLen = 4;
|
||
else
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
|
||
return;
|
||
}
|
||
else if((st_usbDeviceRequest.wValue & 0x00FF) == 0x01) // index = 1
|
||
{
|
||
Memcpy(USBbuf,(u8 *)usbManufacturerStringDescriptor,usbManufacturerStringDescriptor[0]);
|
||
|
||
if((st_usbDeviceRequest.wLength != 0xFF) && (st_usbDeviceRequest.wLength != 0x01F4) && (st_usbDeviceRequest.wLength < usbManufacturerStringDescriptor[0]))
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
else
|
||
g_sUsbEp0TxLen = usbManufacturerStringDescriptor[0];
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
return;
|
||
}
|
||
else if((st_usbDeviceRequest.wValue & 0x00FF) == 0x02) // index = 2
|
||
{
|
||
if(g_bUsbDevType == USBDEVTYPE_HIDPOS)
|
||
{
|
||
usbProductStringDescriptor = (u8*)usbHidPosProductStringDescriptor;
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_HIDKBD)
|
||
{
|
||
usbProductStringDescriptor = (u8*)usbHidKbdProductStringDescriptor;
|
||
}
|
||
else if(g_bUsbDevType == USBDEVTYPE_VSP)
|
||
{
|
||
usbProductStringDescriptor = (u8*)usbVspProductStringDescriptor;
|
||
}
|
||
|
||
Memcpy(USBbuf,(u8 *)usbProductStringDescriptor,usbProductStringDescriptor[0]);
|
||
|
||
if((st_usbDeviceRequest.wLength != 0xFF) && (st_usbDeviceRequest.wLength != 0x01F4) && (st_usbDeviceRequest.wLength < usbProductStringDescriptor[0]))
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
else
|
||
g_sUsbEp0TxLen = usbProductStringDescriptor[0];
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
return;
|
||
}
|
||
else if((st_usbDeviceRequest.wValue & 0x00FF) == 0x03) // index = 3
|
||
{
|
||
Memcpy(USBbuf,(u8 *)usbSerialNumberStringDescriptor,usbSerialNumberStringDescriptor[0]);
|
||
|
||
if((st_usbDeviceRequest.wLength != 0xFF) && (st_usbDeviceRequest.wLength != 0x01F4) && (st_usbDeviceRequest.wLength < usbSerialNumberStringDescriptor[0]))
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
else
|
||
g_sUsbEp0TxLen = usbSerialNumberStringDescriptor[0];
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
return;
|
||
}
|
||
#if CCID
|
||
else //if((st_usbDeviceRequest.wValue & 0x00FF) == 0x04)
|
||
{
|
||
Memcpy(USBbuf,(u8 *)ccidReaderStringDescriptor,ccidReaderStringDescriptor[0]);
|
||
if((st_usbDeviceRequest.wLength < ccidReaderStringDescriptor[0]))
|
||
g_sUsbEp0TxLen = st_usbDeviceRequest.wLength;
|
||
else
|
||
g_sUsbEp0TxLen = ccidReaderStringDescriptor[0];
|
||
|
||
usbEpnTxAll(0, USBbuf, g_sUsbEp0TxLen);
|
||
return;
|
||
}
|
||
#endif
|
||
else
|
||
{
|
||
EP0SENDSTALL;//修改苹果兼容性, 因为键盘的接口描述符字符串索引,给了0x07,但是苹果设备获取此字符串,导致,一直在回送nak
|
||
}
|
||
}
|