submit the first version usb device stack.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2344 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
qiuyiuestc@gmail.com
2012-10-11 17:06:30 +00:00
parent 051fd8eca9
commit c752d9b095
13 changed files with 1566 additions and 399 deletions
@@ -0,0 +1,15 @@
Import('RTT_ROOT')
from building import *
cwd = GetCurrentDir()
src = Split("""
core/core.c
core/usbdevice.c
class/cdc_vcom.c
""")
CPPPATH = [cwd]
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_USB_DEVICE'], CPPPATH = CPPPATH)
Return('group')
@@ -0,0 +1,157 @@
/*
* File : cdc.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-10-03 Yi Qiu first version
*/
#define USB_CDC_PRODUCT_ID 0x5740 /* Product ID */
#define USB_CDC_BUFSIZE 0x40
#define USB_CDC_CLASS_COMM 0x02
#define USB_CDC_CLASS_DATA 0x0A
#define USB_CDC_SUBCLASS_DLCM 0x01
#define USB_CDC_SUBCLASS_ACM 0x02
#define USB_CDC_SUBCLASS_TCM 0x03
#define USB_CDC_SUBCLASS_MCCM 0x04
#define USB_CDC_SUBCLASS_CCM 0x05
#define USB_CDC_SUBCLASS_ETH 0x06
#define USB_CDC_SUBCLASS_ATM 0x07
#define USB_CDC_PROTOCOL_V25TER 0x01
#define USB_CDC_PROTOCOL_I430 0x30
#define USB_CDC_PROTOCOL_HDLC 0x31
#define USB_CDC_PROTOCOL_TRANS 0x32
#define USB_CDC_PROTOCOL_Q921M 0x50
#define USB_CDC_PROTOCOL_Q921 0x51
#define USB_CDC_PROTOCOL_Q921TM 0x52
#define USB_CDC_PROTOCOL_V42BIS 0x90
#define USB_CDC_PROTOCOL_Q931 0x91
#define USB_CDC_PROTOCOL_V120 0x92
#define USB_CDC_PROTOCOL_CAPI20 0x93
#define USB_CDC_PROTOCOL_HOST 0xFD
#define USB_CDC_PROTOCOL_PUFD 0xFE
#define USB_CDC_CS_INTERFACE 0x24
#define USB_CDC_CS_ENDPOINT 0x25
#define USB_CDC_SCS_HEADER 0x00
#define USB_CDC_SCS_CALL_MGMT 0x01
#define USB_CDC_SCS_ACM 0x02
#define USB_CDC_SCS_UNION 0x06
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
#define CDC_SET_COMM_FEATURE 0x02
#define CDC_GET_COMM_FEATURE 0x03
#define CDC_CLEAR_COMM_FEATURE 0x04
#define CDC_SET_AUX_LINE_STATE 0x10
#define CDC_SET_HOOK_STATE 0x11
#define CDC_PULSE_SETUP 0x12
#define CDC_SEND_PULSE 0x13
#define CDC_SET_PULSE_TIME 0x14
#define CDC_RING_AUX_JACK 0x15
#define CDC_SET_LINE_CODING 0x20
#define CDC_GET_LINE_CODING 0x21
#define CDC_SET_CONTROL_LINE_STATE 0x22
#define CDC_SEND_BREAK 0x23
#define CDC_SET_RINGER_PARMS 0x30
#define CDC_GET_RINGER_PARMS 0x31
#define CDC_SET_OPERATION_PARMS 0x32
#define CDC_GET_OPERATION_PARMS 0x33
#define CDC_SET_LINE_PARMS 0x34
#define CDC_GET_LINE_PARMS 0x35
#define CDC_DIAL_DIGITS 0x36
#define CDC_SET_UNIT_PARAMETER 0x37
#define CDC_GET_UNIT_PARAMETER 0x38
#define CDC_CLEAR_UNIT_PARAMETER 0x39
#define CDC_GET_PROFILE 0x3A
#define CDC_SET_ETH_MULTICAST_FILTERS 0x40
#define CDC_SET_ETH_POWER_MGMT_FILT 0x41
#define CDC_GET_ETH_POWER_MGMT_FILT 0x42
#define CDC_SET_ETH_PACKET_FILTER 0x43
#define CDC_GET_ETH_STATISTIC 0x44
#define CDC_SET_ATM_DATA_FORMAT 0x50
#define CDC_GET_ATM_DEVICE_STATISTICS 0x51
#define CDC_SET_ATM_DEFAULT_VC 0x52
#define CDC_GET_ATM_VC_STATISTICS 0x53
#pragma pack(1)
struct ucdc_header_descriptor
{
rt_uint8_t length;
rt_uint8_t type;
rt_uint8_t subtype;
rt_uint16_t bcd;
};
typedef struct ucdc_header_descriptor* ucdc_hdr_desc_t;
struct ucdc_acm_descriptor
{
rt_uint8_t length;
rt_uint8_t type;
rt_uint8_t subtype;
rt_uint8_t capabilties;
};
typedef struct ucdc_acm_descriptor* ucdc_acm_desc_t;
struct ucdc_call_mgmt_descriptor
{
rt_uint8_t length;
rt_uint8_t type;
rt_uint8_t subtype;
rt_uint8_t capabilties;
rt_uint8_t data_interface;
};
typedef struct ucdc_call_mgmt_descriptor* ucdc_call_mgmt_desc_t;
struct ucdc_union_descriptor
{
rt_uint8_t length;
rt_uint8_t type;
rt_uint8_t subtype;
rt_uint8_t master_interface;
rt_uint8_t slave_interface0;
};
typedef struct ucdc_union_descriptor* ucdc_union_desc_t;
struct ucdc_comm_descriptor
{
struct uinterface_descriptor intf_desc;
struct ucdc_header_descriptor hdr_desc;
struct ucdc_call_mgmt_descriptor call_mgmt_desc;
struct ucdc_acm_descriptor acm_desc;
struct ucdc_union_descriptor union_desc;
struct uendpoint_descriptor ep_desc;
};
typedef struct ucdc_comm_descriptor* ucdc_comm_desc_t;
struct ucdc_data_descriptor
{
struct uinterface_descriptor intf_desc;
struct uendpoint_descriptor ep_out_desc;
struct uendpoint_descriptor ep_in_desc;
};
typedef struct ucdc_data_descriptor* ucdc_data_desc_t;
struct ucdc_line_coding
{
rt_uint32_t dwDTERate;
rt_uint8_t bCharFormat;
rt_uint8_t bParityType;
rt_uint8_t bDataBits;
};
typedef struct ucdc_line_coding* ucdc_line_coding_t;
#pragma pack()
File diff suppressed because it is too large Load Diff
+47 -18
View File
@@ -101,7 +101,7 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
if(index > USB_STRING_INTERFACE_INDEX)
{
rt_kprintf("unknown string index\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
return -RT_ERROR;
}
if(index == 0)
@@ -158,14 +158,14 @@ static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
break;
default:
rt_kprintf("unknown descriptor\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
}
else
{
rt_kprintf("request direction error\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
}
return RT_EOK;
@@ -388,7 +388,7 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
_get_descriptor(device, setup);
break;
case USB_REQ_SET_DESCRIPTOR:
dcd_ep0_stall(dcd);
dcd_ep_stall(dcd, 0);
break;
case USB_REQ_GET_CONFIGURATION:
_get_config(device, setup);
@@ -398,7 +398,7 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
break;
default:
rt_kprintf("unknown device request\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
break;
@@ -413,7 +413,7 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
break;
default:
rt_kprintf("unknown interface request\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
break;
@@ -434,17 +434,17 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
break;
default:
rt_kprintf("unknown endpoint request\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
break;
case USB_REQ_TYPE_OTHER:
rt_kprintf("unknown other type request\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
default:
rt_kprintf("unknown type request\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
@@ -470,7 +470,7 @@ static rt_err_t _class_request(udevice_t device, ureq_t setup)
/* verify request value */
if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
{
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
return -RT_ERROR;
}
@@ -484,7 +484,7 @@ static rt_err_t _class_request(udevice_t device, ureq_t setup)
break;
default:
rt_kprintf("unknown class request type\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
break;
}
@@ -526,13 +526,39 @@ static rt_err_t _setup_request(udevice_t device, ureq_t setup)
break;
default:
rt_kprintf("unknown setup request type\n");
dcd_ep0_stall(device->dcd);
dcd_ep_stall(device->dcd, 0);
return -RT_ERROR;
}
return RT_EOK;
}
/**
* This function will notity sof event to all of class.
*
* @param device the usb device object.
*
* @return RT_EOK.
*/
rt_err_t _sof_notify(udevice_t device)
{
struct rt_list_node *i;
uclass_t cls;
RT_ASSERT(device != RT_NULL);
/* to notity every class that sof event comes */
for (i=device->curr_cfg->cls_list.next;
i!=&device->curr_cfg->cls_list; i=i->next)
{
cls = (uclass_t)rt_list_entry(i, struct uclass, list);
if(cls->ops->sof_handler != RT_NULL)
cls->ops->sof_handler(device);
}
return RT_EOK;
}
/**
* This function will create an usb device object.
*
@@ -797,8 +823,6 @@ udevice_t rt_usbd_find_device(udcd_t dcd)
{
struct rt_list_node* node;
udevice_t device;
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_device\n"));
/* parameter check */
RT_ASSERT(dcd != RT_NULL);
@@ -1180,15 +1204,13 @@ static void rt_usbd_thread_entry(void* parameter)
/* receive message */
if(rt_mq_recv(usb_mq, &msg, sizeof(struct udev_msg), RT_WAITING_FOREVER)
!= RT_EOK ) continue;
//RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
switch (msg.type)
{
case USB_MSG_SETUP_NOTIFY:
device = rt_usbd_find_device(msg.content.setup_msg.dcd);
device = rt_usbd_find_device(msg.dcd);
if(device != RT_NULL)
_setup_request(device, (ureq_t)msg.content.setup_msg.packet);
_setup_request(device, (ureq_t)msg.content.setup_msg.packet);
else
rt_kprintf("invalid usb device\n");
break;
@@ -1199,6 +1221,13 @@ static void rt_usbd_thread_entry(void* parameter)
else
rt_kprintf("invalid endpoint\n");
break;
case USB_MSG_SOF:
device = rt_usbd_find_device(msg.dcd);
if(device != RT_NULL)
_sof_notify(device);
else
rt_kprintf("invalid usb device\n");
break;
default:
break;
}
@@ -0,0 +1,86 @@
/*
* File : usbdevice.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-10-02 Yi Qiu first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rtservice.h>
#define RT_USB_DEVICE_CDC
const static char* ustring[] =
{
"Language",
"RT-Thread Team.",
"UDISK",
"12345678",
"Config",
"Interface",
};
rt_err_t rt_usb_device_init(const char* udc_name)
{
rt_device_t udc;
udevice_t udevice;
uconfig_t cfg;
uclass_t cls;
RT_ASSERT(udc_name != RT_NULL);
udc = rt_device_find(udc_name);
if(udc == RT_NULL)
{
rt_kprintf("can't find usb device controller %s\n", udc_name);
return -RT_ERROR;
}
/* create and startup usb device thread */
rt_usbd_core_init();
/* create a device object */
udevice = rt_usbd_device_create(ustring);
/* set usb controller driver to the device */
rt_usbd_device_set_controller(udevice, (udcd_t)udc);
/* create a configuration object */
cfg = rt_usbd_config_create();
#if defined RT_USB_DEVICE_MASS_STORAGE
/* create a mass storage class object */
cls = rt_usbd_class_mass_storage_create(udevice);
#elif defined RT_USB_DEVICE_CDC
/* create a cdc class object */
cls = rt_usbd_class_cdc_create(udevice);
#else
#error
#endif
/* set device descriptor to the device */
rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
/* add the mass storage class to the configuration */
rt_usbd_config_add_class(cfg, cls);
/* add the configuration to the device */
rt_usbd_device_add_config(udevice, cfg);
/* set default configuration to 1 */
rt_usbd_set_config(udevice, 1);
/* initialize usb device controller */
rt_device_init(udc);
return RT_EOK;
}
+25 -19
View File
@@ -19,17 +19,23 @@
#ifdef RT_USB_CLASS_ADK
static struct uclass_driver adk_driver;
static const char* _adk_manufacturer = RT_NULL;
static const char* _adk_model = RT_NULL;
static const char* _adk_description = RT_NULL;
static const char* _adk_version = RT_NULL;
static const char* _adk_uri = RT_NULL;
static const char* _adk_serial = RT_NULL;
rt_err_t rt_usb_adk_set_string(const char* manufacturer, const char* model,
const char* description, const char* version, const char* uri,
const char* description, const char* _version, const char* uri,
const char* serial)
{
adk_driver.manufacturer = manufacturer;
adk_driver.model = model;
adk_driver.description = description;
adk_driver.version = version;
adk_driver.uri = uri;
adk_driver.serial = serial;
_adk_manufacturer = manufacturer;
_adk_model = model;
_adk_description = description;
_adk_version = _version;
_adk_uri = uri;
_adk_serial = serial;
return RT_EOK;
}
@@ -245,24 +251,24 @@ static rt_err_t rt_usb_adk_run(void* arg)
}
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_MANUFACTURER, adk_driver.manufacturer);
ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_MODEL, adk_driver.model);
ACCESSORY_STRING_MODEL, _adk_model);
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_DESCRIPTION, adk_driver.description);
ACCESSORY_STRING_DESCRIPTION, _adk_description);
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_VERSION, adk_driver.version);
ACCESSORY_STRING_VERSION, _adk_version);
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_URI, adk_driver.uri);
ACCESSORY_STRING_URI, _adk_uri);
rt_usb_adk_send_string(ifinst,
ACCESSORY_STRING_SERIAL, adk_driver.serial);
ACCESSORY_STRING_SERIAL, _adk_serial);
RT_DEBUG_LOG(RT_DEBUG_USB,("manufacturer %s\n", adk_driver.manufacturer));
RT_DEBUG_LOG(RT_DEBUG_USB,("model %s\n", adk_driver.model));
RT_DEBUG_LOG(RT_DEBUG_USB,("description %s\n", adk_driver.description));
RT_DEBUG_LOG(RT_DEBUG_USB,("version %s\n", adk_driver.version));
RT_DEBUG_LOG(RT_DEBUG_USB,("uri %s\n", adk_driver.uri));
RT_DEBUG_LOG(RT_DEBUG_USB,("serial %s\n", adk_driver.serial));
RT_DEBUG_LOG(RT_DEBUG_USB,("manufacturer %s\n", _adk_manufacturer));
RT_DEBUG_LOG(RT_DEBUG_USB,("model %s\n", _adk_model));
RT_DEBUG_LOG(RT_DEBUG_USB,("description %s\n", _adk_description));
RT_DEBUG_LOG(RT_DEBUG_USB,("version %s\n", _adk_version));
RT_DEBUG_LOG(RT_DEBUG_USB,("uri %s\n", _adk_uri));
RT_DEBUG_LOG(RT_DEBUG_USB,("serial %s\n", _adk_serial));
if((ret = rt_usb_adk_start(ifinst)) != RT_EOK)
{
@@ -40,8 +40,6 @@ rt_err_t rt_usb_class_driver_init(void)
rt_err_t rt_usb_class_driver_register(ucd_t drv)
{
RT_ASSERT(drv != RT_NULL);
if (drv == RT_NULL) return -RT_ERROR;
/* insert class driver into driver list */
+3 -3
View File
@@ -545,10 +545,10 @@ static void rt_usb_hub_thread_entry(void* parameter)
{
while(1)
{
struct umsg msg;
struct uhost_msg msg;
/* receive message */
if(rt_mq_recv(usb_mq, &msg, sizeof(struct umsg), RT_WAITING_FOREVER)
if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
!= RT_EOK ) continue;
RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
@@ -576,7 +576,7 @@ static void rt_usb_hub_thread_entry(void* parameter)
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usb_post_event(struct umsg* msg, rt_size_t size)
rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size)
{
RT_ASSERT(msg != RT_NULL);
+28 -28
View File
@@ -9,7 +9,7 @@
*
* Change Logs:
* Date Author Notes
* 2011-3-12 Yi Qiu first version
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
@@ -26,49 +26,49 @@
*/
void rt_usb_host_init(void)
{
ucd_t drv;
ucd_t drv;
#ifdef RT_USB_CLASS_HID
uprotocal_t protocal;
uprotocal_t protocal;
#endif
/* initialize usb hub thread */
rt_usb_hub_thread();
/* initialize usb hub thread */
rt_usb_hub_thread();
/* initialize class driver */
rt_usb_class_driver_init();
/* initialize class driver */
rt_usb_class_driver_init();
#ifdef RT_USB_CLASS_MASS_STORAGE
/* register mass storage class driver */
drv = rt_usb_class_driver_storage();
rt_usb_class_driver_register(drv);
/* register mass storage class driver */
drv = rt_usb_class_driver_storage();
rt_usb_class_driver_register(drv);
#endif
#ifdef RT_USB_CLASS_HID
/* register hid class driver */
drv = rt_usb_class_driver_hid();
rt_usb_class_driver_register(drv);
/* register hid class driver */
drv = rt_usb_class_driver_hid();
rt_usb_class_driver_register(drv);
#ifdef RT_USB_HID_KEYBOARD
/* register hid keyboard protocal */
protocal = rt_usb_hid_protocal_kbd();
rt_usb_hid_protocal_register(protocal);
#ifdef RT_USB_HID_KEYBOARD
/* register hid keyboard protocal */
protocal = rt_usb_hid_protocal_kbd();
rt_usb_hid_protocal_register(protocal);
#endif
#ifdef RT_USB_HID_MOUSE
/* register hid mouse protocal */
protocal = rt_usb_hid_protocal_mouse();
rt_usb_hid_protocal_register(protocal);
#endif
#ifdef RT_USB_HID_MOUSE
/* register hid mouse protocal */
protocal = rt_usb_hid_protocal_mouse();
rt_usb_hid_protocal_register(protocal);
#endif
#endif
#ifdef RT_USB_CLASS_ADK
/* register adk class driver */
drv = rt_usb_class_driver_adk();
rt_usb_class_driver_register(drv);
/* register adk class driver */
drv = rt_usb_class_driver_adk();
rt_usb_class_driver_register(drv);
#endif
/* register hub class driver */
drv = rt_usb_class_driver_hub();
rt_usb_class_driver_register(drv);
/* register hub class driver */
drv = rt_usb_class_driver_hub();
rt_usb_class_driver_register(drv);
}