Update USB Device Stack to support more device controllers;
Pass USB CV test verification; Code cleanup;
This commit is contained in:
parent
db02f56283
commit
8fd0a7f9c6
|
@ -20,6 +20,7 @@
|
|||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
* 2013-04-26 aozima add DEVICEQUALIFIER support.
|
||||
*/
|
||||
|
||||
#ifndef __USB_COMMON_H__
|
||||
|
@ -148,13 +149,22 @@ extern "C" {
|
|||
#define USB_EPNO_MASK 0x7f
|
||||
#define USB_DIR_OUT 0x00
|
||||
#define USB_DIR_IN 0x80
|
||||
#define USB_DIR_INOUT 0x40
|
||||
#define USB_DIR_MASK 0x80
|
||||
|
||||
#define ID_UNASSIGNED 0
|
||||
#define ID_ASSIGNED 1
|
||||
|
||||
#define RH_GET_PORT_STATUS 0
|
||||
#define RH_SET_PORT_STATUS 1
|
||||
#define RH_CLEAR_PORT_FEATURE 2
|
||||
#define RH_SET_PORT_FEATURE 3
|
||||
|
||||
#define USB_BUS_POWERED 0
|
||||
#define USB_SELF_POWERED 1
|
||||
#define USB_REMOTE_WAKEUP 1
|
||||
#define USB_EP_HALT 0
|
||||
|
||||
/*
|
||||
* Port feature numbers
|
||||
*/
|
||||
|
@ -205,6 +215,7 @@ extern "C" {
|
|||
|
||||
#define USB_EP_ATTR(attr) (attr & USB_EP_ATTR_TYPE_MASK)
|
||||
#define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
|
||||
#define USB_EP_DIR(addr) ((addr & USB_DIR_MASK)>>7)
|
||||
|
||||
#define uswap_32(x) \
|
||||
((((x) & 0xff000000) >> 24) | \
|
||||
|
@ -332,6 +343,21 @@ struct uhub_descriptor
|
|||
};
|
||||
typedef struct uhub_descriptor* uhub_desc_t;
|
||||
|
||||
/* USB_DESC_TYPE_DEVICEQUALIFIER: Device Qualifier descriptor */
|
||||
struct usb_qualifier_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t bDescriptorType;
|
||||
|
||||
rt_uint16_t bcdUSB; // TODO: big-endian.
|
||||
rt_uint8_t bDeviceClass;
|
||||
rt_uint8_t bDeviceSubClass;
|
||||
rt_uint8_t bDeviceProtocol;
|
||||
rt_uint8_t bMaxPacketSize0;
|
||||
rt_uint8_t bNumConfigurations;
|
||||
rt_uint8_t bRESERVED;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
struct uhid_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
|
@ -357,8 +383,10 @@ struct ureqest
|
|||
};
|
||||
typedef struct ureqest* ureq_t;
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a < b ? a : b)
|
||||
#define MAX(a, b) (a > b ? a : b)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* the define related to mass storage
|
||||
|
@ -368,6 +396,11 @@ typedef struct ureqest* ureq_t;
|
|||
|
||||
#define SIZEOF_CSW 0x0d
|
||||
#define SIZEOF_CBW 0x1f
|
||||
#define SIZEOF_INQUIRY_CMD 0x24
|
||||
#define SIZEOF_MODE_SENSE_6 0x4
|
||||
#define SIZEOF_READ_CAPACITIES 0xc
|
||||
#define SIZEOF_READ_CAPACITY 0x8
|
||||
#define SIZEOF_REQUEST_SENSE 0x12
|
||||
|
||||
#define CBWFLAGS_DIR_M 0x80
|
||||
#define CBWFLAGS_DIR_IN 0x80
|
||||
|
@ -376,7 +409,7 @@ typedef struct ureqest* ureq_t;
|
|||
#define SCSI_TEST_UNIT_READY 0x00
|
||||
#define SCSI_REQUEST_SENSE 0x03
|
||||
#define SCSI_INQUIRY_CMD 0x12
|
||||
#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
|
||||
#define SCSI_ALLOW_REMOVAL 0x1e
|
||||
#define SCSI_MODE_SENSE_6 0x1a
|
||||
#define SCSI_START_STOP 0x1b
|
||||
#define SCSI_READ_CAPACITIES 0x23
|
||||
|
@ -405,7 +438,7 @@ struct ustorage_csw
|
|||
{
|
||||
rt_uint32_t signature;
|
||||
rt_uint32_t tag;
|
||||
rt_uint32_t data_reside;
|
||||
rt_int32_t data_reside;
|
||||
rt_uint8_t status;
|
||||
};
|
||||
typedef struct ustorage_csw* ustorage_csw_t;
|
||||
|
|
|
@ -20,7 +20,8 @@
|
|||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
* 2012-12-12 heyuanjie87 change endpoint and class handler
|
||||
* 2012-12-12 heyuanjie87 change endpoint and function handler
|
||||
* 2013-04-26 aozima add DEVICEQUALIFIER support.
|
||||
*/
|
||||
|
||||
#ifndef __USB_DEVICE_H__
|
||||
|
@ -44,46 +45,102 @@
|
|||
|
||||
#define USB_BCD_DEVICE 0x0200 /* USB Specification Release Number in Binary-Coded Decimal */
|
||||
#define USB_BCD_VERSION 0x0200 /* USB 2.0 */
|
||||
#define EP0_IN_ADDR 0x80
|
||||
#define EP0_OUT_ADDR 0x00
|
||||
#define EP_HANDLER(ep, func, size) RT_ASSERT(ep != RT_NULL); ep->handler(func, size)
|
||||
#define EP_ADDRESS(ep) ep->ep_desc->bEndpointAddress
|
||||
#define EP_MAXPACKET(ep) ep->ep_desc->wMaxPacketSize
|
||||
#define FUNC_ENABLE(func) do{ \
|
||||
if(func->ops->enable != RT_NULL && \
|
||||
func->enabled == RT_FALSE) \
|
||||
{ \
|
||||
if(func->ops->enable(func) == RT_EOK) \
|
||||
func->enabled = RT_TRUE; \
|
||||
} \
|
||||
}while(0)
|
||||
#define FUNC_DISABLE(func) do{ \
|
||||
if(func->ops->disable != RT_NULL && \
|
||||
func->enabled == RT_TRUE) \
|
||||
{ \
|
||||
func->enabled = RT_FALSE; \
|
||||
func->ops->disable(func); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
struct uclass;
|
||||
struct ufunction;
|
||||
struct udevice;
|
||||
struct uendpoint;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
/* request to read full count */
|
||||
UIO_REQUEST_READ_FULL,
|
||||
/* request to read any count */
|
||||
UIO_REQUEST_READ_BEST,
|
||||
/* request to write full count */
|
||||
UIO_REQUEST_WRITE,
|
||||
}UIO_REQUEST_TYPE;
|
||||
|
||||
struct udcd_ops
|
||||
{
|
||||
rt_err_t (*set_address)(rt_uint8_t value);
|
||||
rt_err_t (*clear_feature)(rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t (*set_feature)(rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t (*ep_alloc)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_free)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_stall)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_run)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_stop)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_read)(struct uendpoint* ep, void *buffer, rt_size_t size);
|
||||
rt_size_t (*ep_write)(struct uendpoint* ep, void *buffer, rt_size_t size);
|
||||
rt_err_t (*send_status)(void);
|
||||
rt_err_t (*set_address)(rt_uint8_t address);
|
||||
rt_err_t (*set_config)(rt_uint8_t address);
|
||||
rt_err_t (*ep_set_stall)(rt_uint8_t address);
|
||||
rt_err_t (*ep_clear_stall)(rt_uint8_t address);
|
||||
rt_err_t (*ep_enable)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_disable)(struct uendpoint* ep);
|
||||
rt_size_t (*ep_read_prepare)(rt_uint8_t address, void *buffer, rt_size_t size);
|
||||
rt_size_t (*ep_read)(rt_uint8_t address, void *buffer);
|
||||
rt_size_t (*ep_write)(rt_uint8_t address, void *buffer, rt_size_t size);
|
||||
rt_err_t (*ep0_send_status)(void);
|
||||
rt_err_t (*suspend)(void);
|
||||
rt_err_t (*wakeup)(void);
|
||||
};
|
||||
|
||||
struct udcd
|
||||
struct ep_id
|
||||
{
|
||||
struct rt_device parent;
|
||||
struct udcd_ops* ops;
|
||||
struct rt_completion completion;
|
||||
rt_uint8_t addr;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t dir;
|
||||
rt_uint8_t maxpacket;
|
||||
rt_uint8_t status;
|
||||
};
|
||||
typedef struct udcd* udcd_t;
|
||||
|
||||
typedef rt_err_t (*udep_handler_t)(struct udevice* device, struct uclass* cls, rt_size_t size);
|
||||
typedef rt_err_t (*udep_handler_t)(struct ufunction* func, rt_size_t size);
|
||||
|
||||
struct uio_request
|
||||
{
|
||||
rt_list_t list;
|
||||
UIO_REQUEST_TYPE req_type;
|
||||
rt_uint8_t* buffer;
|
||||
rt_size_t size;
|
||||
rt_size_t remain_size;
|
||||
};
|
||||
typedef struct uio_request* uio_request_t;
|
||||
|
||||
struct uendpoint
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t* buffer;
|
||||
uep_desc_t ep_desc;
|
||||
rt_list_t request_list;
|
||||
struct uio_request request;
|
||||
rt_uint8_t* buffer;
|
||||
rt_bool_t stalled;
|
||||
struct ep_id* id;
|
||||
udep_handler_t handler;
|
||||
rt_bool_t is_stall;
|
||||
rt_err_t (*rx_indicate)(struct udevice* dev, rt_size_t size);
|
||||
};
|
||||
typedef struct uendpoint* uep_t;
|
||||
|
||||
struct udcd
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct udcd_ops* ops;
|
||||
struct uendpoint ep0;
|
||||
struct ep_id* ep_pool;
|
||||
};
|
||||
typedef struct udcd* udcd_t;
|
||||
|
||||
struct ualtsetting
|
||||
{
|
||||
rt_list_t list;
|
||||
|
@ -94,7 +151,7 @@ struct ualtsetting
|
|||
};
|
||||
typedef struct ualtsetting* ualtsetting_t;
|
||||
|
||||
typedef rt_err_t (*uintf_handler_t)(struct udevice* device, struct uclass* cls, ureq_t setup);
|
||||
typedef rt_err_t (*uintf_handler_t)(struct ufunction* func, ureq_t setup);
|
||||
|
||||
struct uinterface
|
||||
{
|
||||
|
@ -106,32 +163,32 @@ struct uinterface
|
|||
};
|
||||
typedef struct uinterface* uintf_t;
|
||||
|
||||
struct uclass_ops
|
||||
struct ufunction_ops
|
||||
{
|
||||
rt_err_t (*run)(struct udevice* device, struct uclass* cls);
|
||||
rt_err_t (*stop)(struct udevice* device, struct uclass* cls);
|
||||
rt_err_t (*sof_handler)(struct udevice* device, struct uclass* cls);
|
||||
rt_err_t (*enable)(struct ufunction* func);
|
||||
rt_err_t (*disable)(struct ufunction* func);
|
||||
rt_err_t (*sof_handler)(struct ufunction* func);
|
||||
};
|
||||
typedef struct uclass_ops* uclass_ops_t;
|
||||
typedef struct ufunction_ops* ufunction_ops_t;
|
||||
|
||||
struct uclass
|
||||
struct ufunction
|
||||
{
|
||||
rt_list_t list;
|
||||
uclass_ops_t ops;
|
||||
void* eps;
|
||||
ufunction_ops_t ops;
|
||||
struct udevice* device;
|
||||
udev_desc_t dev_desc;
|
||||
void* user_data;
|
||||
rt_bool_t enabled;
|
||||
|
||||
rt_list_t intf_list;
|
||||
};
|
||||
typedef struct uclass* uclass_t;
|
||||
typedef struct ufunction* ufunction_t;
|
||||
|
||||
struct uconfig
|
||||
{
|
||||
rt_list_t list;
|
||||
struct uconfig_descriptor cfg_desc;
|
||||
rt_list_t cls_list;
|
||||
rt_list_t func_list;
|
||||
};
|
||||
typedef struct uconfig* uconfig_t;
|
||||
|
||||
|
@ -139,6 +196,8 @@ struct udevice
|
|||
{
|
||||
rt_list_t list;
|
||||
struct udevice_descriptor dev_desc;
|
||||
|
||||
struct usb_qualifier_descriptor * dev_qualifier;
|
||||
const char** str;
|
||||
|
||||
udevice_state_t state;
|
||||
|
@ -154,8 +213,11 @@ enum udev_msg_type
|
|||
{
|
||||
USB_MSG_SETUP_NOTIFY,
|
||||
USB_MSG_DATA_NOTIFY,
|
||||
USB_MSG_EP0_OUT,
|
||||
USB_MSG_EP_CLEAR_FEATURE,
|
||||
USB_MSG_SOF,
|
||||
USB_MSG_RESET,
|
||||
USB_MSG_PLUG_IN,
|
||||
/* we don't need to add a "PLUG_IN" event because after the cable is
|
||||
* plugged in(before any SETUP) the classed have nothing to do. If the host
|
||||
* is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
|
||||
|
@ -164,153 +226,184 @@ enum udev_msg_type
|
|||
};
|
||||
typedef enum udev_msg_type udev_msg_type;
|
||||
|
||||
struct ep_msg
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_uint8_t ep_addr;
|
||||
};
|
||||
|
||||
struct udev_msg
|
||||
{
|
||||
udev_msg_type type;
|
||||
udcd_t dcd;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_uint8_t ep_addr;
|
||||
} ep_msg;
|
||||
struct
|
||||
{
|
||||
rt_uint32_t* packet;
|
||||
} setup_msg;
|
||||
struct ep_msg ep_msg;
|
||||
struct ureqest setup;
|
||||
} content;
|
||||
};
|
||||
typedef struct udev_msg* udev_msg_t;
|
||||
|
||||
udevice_t rt_usbd_device_create(void);
|
||||
uconfig_t rt_usbd_config_create(void);
|
||||
uclass_t rt_usbd_class_create(udevice_t device,
|
||||
udev_desc_t dev_desc,
|
||||
uclass_ops_t ops);
|
||||
uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler);
|
||||
uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler);
|
||||
ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size);
|
||||
udevice_t rt_usbd_device_new(void);
|
||||
uconfig_t rt_usbd_config_new(void);
|
||||
ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
|
||||
ufunction_ops_t ops);
|
||||
uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler);
|
||||
uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler);
|
||||
ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size);
|
||||
|
||||
rt_err_t rt_usbd_core_init(void);
|
||||
rt_err_t rt_usb_device_init(const char *udc_name);
|
||||
rt_err_t rt_usbd_post_event(struct udev_msg *msg, rt_size_t size);
|
||||
rt_err_t rt_usbd_free_device(udevice_t device);
|
||||
rt_err_t rt_usb_device_init(void);
|
||||
rt_err_t rt_usbd_event_signal(struct udev_msg* msg);
|
||||
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd);
|
||||
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc);
|
||||
rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
|
||||
rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier);
|
||||
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg);
|
||||
rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls);
|
||||
rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf);
|
||||
rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func);
|
||||
rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf);
|
||||
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting);
|
||||
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep);
|
||||
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting,
|
||||
const void *desc,
|
||||
rt_off_t intf_pos);
|
||||
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos);
|
||||
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value);
|
||||
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value);
|
||||
|
||||
udevice_t rt_usbd_find_device(udcd_t dcd);
|
||||
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value);
|
||||
uintf_t rt_usbd_find_interface(udevice_t device,
|
||||
rt_uint8_t value,
|
||||
uclass_t *pcls);
|
||||
uep_t rt_usbd_find_endpoint(udevice_t device,
|
||||
uclass_t *pcls,
|
||||
rt_uint8_t ep_addr);
|
||||
uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc);
|
||||
uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr);
|
||||
rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req);
|
||||
rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size);
|
||||
rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
|
||||
rt_err_t (*rx_ind)(udevice_t device, rt_size_t size));
|
||||
|
||||
uclass_t rt_usbd_class_mstorage_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_cdc_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_rndis_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_dap_create(udevice_t device);
|
||||
ufunction_t rt_usbd_function_mstorage_create(udevice_t device);
|
||||
ufunction_t rt_usbd_function_cdc_create(udevice_t device);
|
||||
ufunction_t rt_usbd_function_rndis_create(udevice_t device);
|
||||
ufunction_t rt_usbd_function_dap_create(udevice_t device);
|
||||
|
||||
#ifdef RT_USB_DEVICE_COMPOSITE
|
||||
rt_err_t rt_usbd_class_set_iad(uclass_t cls, uiad_desc_t iad_desc);
|
||||
rt_err_t rt_usbd_function_set_iad(ufunction_t func, uiad_desc_t iad_desc);
|
||||
#endif
|
||||
|
||||
rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t value)
|
||||
rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep);
|
||||
rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep);
|
||||
rt_err_t rt_usbd_ep0_set_stall(udevice_t device);
|
||||
rt_err_t rt_usbd_ep0_clear_stall(udevice_t device);
|
||||
rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct ureqest* setup);
|
||||
rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size);
|
||||
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address);
|
||||
rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size);
|
||||
rt_err_t rt_usbd_reset_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_connect_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_disconnect_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_sof_handler(udcd_t dcd);
|
||||
|
||||
rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->set_address != RT_NULL);
|
||||
|
||||
return dcd->ops->set_address(value);
|
||||
return dcd->ops->set_address(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_clear_feature(udcd_t dcd,
|
||||
rt_uint16_t value,
|
||||
rt_uint16_t index)
|
||||
rt_inline rt_err_t dcd_set_config(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->set_config != RT_NULL);
|
||||
|
||||
return dcd->ops->clear_feature(value, index);
|
||||
return dcd->ops->set_config(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_set_feature(udcd_t dcd,
|
||||
rt_uint8_t value,
|
||||
rt_uint16_t index)
|
||||
rt_inline rt_err_t dcd_ep_enable(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_enable != RT_NULL);
|
||||
|
||||
return dcd->ops->set_feature(value, index);
|
||||
return dcd->ops->ep_enable(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_stall(udcd_t dcd, uep_t ep)
|
||||
rt_inline rt_err_t dcd_ep_disable(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_disable != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_stall(ep);
|
||||
return dcd->ops->ep_disable(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_uint8_t dcd_ep_alloc(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_alloc(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_free(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_free(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_run(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_run(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_stop(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_stop(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_read(udcd_t dcd, uep_t ep, void *buffer,
|
||||
rt_inline rt_size_t dcd_ep_read_prepare(udcd_t dcd, rt_uint8_t address, void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_read(ep, buffer, size);
|
||||
if(dcd->ops->ep_read_prepare != RT_NULL)
|
||||
{
|
||||
return dcd->ops->ep_read_prepare(address, buffer, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_write(udcd_t dcd,
|
||||
uep_t ep,
|
||||
void *buffer,
|
||||
rt_inline rt_size_t dcd_ep_read(udcd_t dcd, rt_uint8_t address, void *buffer)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
|
||||
if(dcd->ops->ep_read != RT_NULL)
|
||||
{
|
||||
return dcd->ops->ep_read(address, buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_write(udcd_t dcd, rt_uint8_t address, void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_write != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_write(ep, buffer, size);
|
||||
return dcd->ops->ep_write(address, buffer, size);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_send_status(udcd_t dcd)
|
||||
rt_inline rt_err_t dcd_ep0_send_status(udcd_t dcd)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep0_send_status != RT_NULL);
|
||||
|
||||
return dcd->ops->send_status();
|
||||
return dcd->ops->ep0_send_status();
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_set_stall(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_set_stall != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_set_stall(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_clear_stall(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_clear_stall != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_clear_stall(address);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -13,9 +13,6 @@ if GetDepend('RT_USB_DEVICE_CDC'):
|
|||
if GetDepend('RT_USB_DEVICE_MSTORAGE'):
|
||||
src += Glob('class/mstorage.c')
|
||||
|
||||
if GetDepend('RT_USB_DEVICE_RNDIS'):
|
||||
src += Glob('class/rndis.c')
|
||||
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_USB_DEVICE'], CPPPATH = CPPPATH)
|
||||
|
|
|
@ -20,34 +20,56 @@
|
|||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-10-02 Yi Qiu first version
|
||||
* 2012-12-12 heyuanjie87 change endpoints and class handler
|
||||
* 2012-12-12 heyuanjie87 change endpoints and function handler
|
||||
* 2013-06-25 heyuanjie87 remove SOF mechinism
|
||||
* 2013-07-20 Yi Qiu do more test
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtservice.h>
|
||||
#include <rtdevice.h>
|
||||
#include <rthw.h>
|
||||
#include "cdc.h"
|
||||
|
||||
#ifdef RT_USB_DEVICE_CDC
|
||||
|
||||
#define TX_TIMEOUT 100
|
||||
#define CDC_RX_BUFSIZE 2048
|
||||
#define CDC_TX_BUFSIZE 2048
|
||||
static rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
|
||||
static rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
|
||||
static struct rt_ringbuffer rx_ringbuffer;
|
||||
static struct rt_ringbuffer tx_ringbuffer;
|
||||
static struct serial_ringbuffer vcom_int_rx;
|
||||
#define CDC_MAX_PACKET_SIZE 64
|
||||
#define VCOM_DEVICE "vcom"
|
||||
|
||||
static struct rt_serial_device vcom_serial;
|
||||
|
||||
#define CDC_MaxPacketSize 64
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t rx_buf[CDC_RX_BUFSIZE];
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t tx_buf[CDC_TX_BUFSIZE];
|
||||
static rt_uint8_t vcom_thread_stack[512];
|
||||
static struct rt_thread vcom_thread;
|
||||
#define VCOM_MQ_MSG_SZ 16
|
||||
#define VCOM_MQ_MAX_MSG 4
|
||||
/* internal of the message queue: every message is associated with a pointer,
|
||||
* so in order to recveive VCOM_MQ_MAX_MSG messages, we have to allocate more
|
||||
* than VCOM_MQ_MSG_SZ*VCOM_MQ_MAX_MSG memery. */
|
||||
static rt_uint8_t vcom_tx_thread_mq_pool[(VCOM_MQ_MSG_SZ+sizeof(void*))*VCOM_MQ_MAX_MSG];
|
||||
static struct rt_messagequeue vcom_tx_thread_mq;
|
||||
static struct ucdc_line_coding line_coding;
|
||||
|
||||
volatile static rt_bool_t vcom_connected = RT_FALSE;
|
||||
volatile static rt_bool_t vcom_in_sending = RT_FALSE;
|
||||
struct vcom
|
||||
{
|
||||
struct rt_serial_device serial;
|
||||
uep_t ep_out;
|
||||
uep_t ep_in;
|
||||
uep_t ep_cmd;
|
||||
rt_bool_t connected;
|
||||
rt_bool_t in_sending;
|
||||
struct rt_completion wait;
|
||||
rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
|
||||
struct rt_ringbuffer rx_ringbuffer;
|
||||
struct serial_ringbuffer vcom_int_rx;
|
||||
};
|
||||
|
||||
struct vcom_tx_msg
|
||||
{
|
||||
struct rt_serial_device * serial;
|
||||
const char *buf;
|
||||
rt_size_t size;
|
||||
};
|
||||
|
||||
static struct udevice_descriptor dev_desc =
|
||||
{
|
||||
|
@ -57,7 +79,7 @@ static struct udevice_descriptor dev_desc =
|
|||
USB_CLASS_CDC, //bDeviceClass;
|
||||
0x00, //bDeviceSubClass;
|
||||
0x00, //bDeviceProtocol;
|
||||
CDC_MaxPacketSize, //bMaxPacketSize0;
|
||||
CDC_MAX_PACKET_SIZE, //bMaxPacketSize0;
|
||||
_VENDOR_ID, //idVendor;
|
||||
_PRODUCT_ID, //idProduct;
|
||||
USB_BCD_DEVICE, //bcdDevice;
|
||||
|
@ -67,6 +89,18 @@ static struct udevice_descriptor dev_desc =
|
|||
USB_DYNAMIC, //bNumConfigurations;
|
||||
};
|
||||
|
||||
static struct usb_qualifier_descriptor dev_qualifier =
|
||||
{
|
||||
sizeof(dev_qualifier),
|
||||
USB_DESC_TYPE_DEVICEQUALIFIER,
|
||||
0x0200,
|
||||
USB_CLASS_CDC,
|
||||
0x00,
|
||||
64,
|
||||
0x01,
|
||||
0,
|
||||
};
|
||||
|
||||
/* communcation interface descriptor */
|
||||
const static struct ucdc_comm_descriptor _comm_desc =
|
||||
{
|
||||
|
@ -156,18 +190,24 @@ const static char* _ustring[] =
|
|||
"Language",
|
||||
"RT-Thread Team.",
|
||||
"RTT Virtual Serial",
|
||||
"1.1.0",
|
||||
"32021919830108",
|
||||
"Configuration",
|
||||
"Interface",
|
||||
};
|
||||
static void rt_usb_vcom_init(struct ufunction *func);
|
||||
|
||||
static void _vcom_reset_state(void)
|
||||
static void _vcom_reset_state(ufunction_t func)
|
||||
{
|
||||
int lvl = rt_hw_interrupt_disable();
|
||||
tx_ringbuffer.read_mirror = tx_ringbuffer.read_index = 0;
|
||||
tx_ringbuffer.write_mirror = tx_ringbuffer.write_index = 0;
|
||||
vcom_connected = RT_FALSE;
|
||||
vcom_in_sending = RT_FALSE;
|
||||
struct vcom* data;
|
||||
int lvl;
|
||||
|
||||
RT_ASSERT(func != RT_NULL)
|
||||
|
||||
data = (struct vcom*)func->user_data;
|
||||
|
||||
lvl = rt_hw_interrupt_disable();
|
||||
data->connected = RT_FALSE;
|
||||
data->in_sending = RT_FALSE;
|
||||
/*rt_kprintf("reset USB serial\n", cnt);*/
|
||||
rt_hw_interrupt_enable(lvl);
|
||||
}
|
||||
|
@ -175,95 +215,73 @@ static void _vcom_reset_state(void)
|
|||
/**
|
||||
* This function will handle cdc bulk in endpoint request.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
* @param func the usb function object.
|
||||
* @param size request size.
|
||||
*
|
||||
* @return RT_EOK.
|
||||
*/
|
||||
static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
||||
static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
rt_uint32_t remain;
|
||||
cdc_eps_t eps;
|
||||
struct vcom *data;
|
||||
|
||||
eps = (cdc_eps_t)cls->eps;
|
||||
level = rt_hw_interrupt_disable();
|
||||
remain = rt_ringbuffer_data_len(&tx_ringbuffer);
|
||||
if (remain != 0)
|
||||
{
|
||||
/* although vcom_in_sending is set in SOF handler in the very
|
||||
* beginning, we have to guarantee the state is right when start
|
||||
* sending. There is at least one extreme case where we have finished the
|
||||
* last IN transaction but the vcom_in_sending is RT_FALSE.
|
||||
*
|
||||
* Ok, what the extreme case is: pour data into vcom in loop. Open
|
||||
* terminal on the PC, you will see the data. Then close it. So the
|
||||
* data will be sent to the PC in the back. When the buffer of the PC
|
||||
* driver is full. It will not send IN packet to the board and you will
|
||||
* have no chance to clear vcom_in_sending in this function. The data
|
||||
* will fill into the ringbuffer until it is full, and we will reset
|
||||
* the state machine and clear vcom_in_sending. When you open the
|
||||
* terminal on the PC again. The IN packet will appear on the line and
|
||||
* we will, eventually, reach here with vcom_in_sending is clear.
|
||||
*/
|
||||
vcom_in_sending = RT_TRUE;
|
||||
rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, remain);
|
||||
rt_hw_interrupt_enable(level);
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
|
||||
/* send data to host */
|
||||
dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, remain);
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler %d\n", size));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
if (size != 0 &&
|
||||
(size % CDC_MaxPacketSize) == 0)
|
||||
data = (struct vcom*)func->user_data;
|
||||
if ((size != 0) && (size % CDC_MAX_PACKET_SIZE == 0))
|
||||
{
|
||||
/* don't have data right now. Send a zero-length-packet to
|
||||
* terminate the transaction.
|
||||
*
|
||||
* FIXME: actually, this might not be the right place to send zlp.
|
||||
* Only the rt_device_write could know how much data is sending. */
|
||||
vcom_in_sending = RT_TRUE;
|
||||
rt_hw_interrupt_enable(level);
|
||||
dcd_ep_write(device->dcd, eps->ep_in, RT_NULL, 0);
|
||||
data->in_sending = RT_TRUE;
|
||||
|
||||
data->ep_in->request.buffer = RT_NULL;
|
||||
data->ep_in->request.size = 0;
|
||||
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
||||
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
vcom_in_sending = RT_FALSE;
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_completion_done(&data->wait);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will handle cdc bulk out endpoint request.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
* @param func the usb function object.
|
||||
* @param size request size.
|
||||
*
|
||||
* @return RT_EOK.
|
||||
*/
|
||||
static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
||||
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
cdc_eps_t eps;
|
||||
struct vcom *data;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
|
||||
eps = (cdc_eps_t)cls->eps;
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
|
||||
|
||||
data = (struct vcom*)func->user_data;
|
||||
/* receive data from USB VCOM */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rt_ringbuffer_put(&rx_ringbuffer, eps->ep_out->buffer, size);
|
||||
rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* notify receive data */
|
||||
rt_hw_serial_isr(&vcom_serial);
|
||||
rt_hw_serial_isr(&data->serial);
|
||||
|
||||
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
|
||||
eps->ep_out->ep_desc->wMaxPacketSize);
|
||||
data->ep_out->request.buffer = data->ep_out->buffer;
|
||||
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
|
||||
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
|
||||
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
@ -276,9 +294,9 @@ static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
|||
*
|
||||
* @return RT_EOK.
|
||||
*/
|
||||
static rt_err_t _ep_cmd_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
||||
static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
|
||||
|
||||
|
@ -301,13 +319,24 @@ static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
|
|||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(setup != RT_NULL);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));
|
||||
|
||||
data.dwDTERate = 115200;
|
||||
data.bCharFormat = 0;
|
||||
data.bDataBits = 8;
|
||||
data.bParityType = 0;
|
||||
size = setup->length > 7 ? 7 : setup->length;
|
||||
|
||||
dcd_ep_write(device->dcd, 0, (void*)&data, size);
|
||||
rt_usbd_ep0_write(device, (void*)&data, size);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _cdc_set_line_coding_callback(udevice_t device, rt_size_t size)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding_callback\n"));
|
||||
|
||||
dcd_ep0_send_status(device->dcd);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
@ -322,21 +351,13 @@ static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
|
|||
*/
|
||||
static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
|
||||
{
|
||||
struct ucdc_line_coding data;
|
||||
rt_err_t ret;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(setup != RT_NULL);
|
||||
|
||||
rt_completion_init(&device->dcd->completion);
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
|
||||
|
||||
dcd_ep_read(device->dcd, 0, (void*)&data, setup->length);
|
||||
|
||||
ret = rt_completion_wait(&device->dcd->completion, 100);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("_cdc_set_line_coding timeout\n");
|
||||
}
|
||||
rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
|
||||
_cdc_set_line_coding_callback);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
@ -349,11 +370,16 @@ static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
|
|||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
|
||||
static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct vcom *data;
|
||||
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
RT_ASSERT(func->device != RT_NULL);
|
||||
RT_ASSERT(setup != RT_NULL);
|
||||
|
||||
data = (struct vcom*)func->user_data;
|
||||
|
||||
switch(setup->request)
|
||||
{
|
||||
case CDC_SEND_ENCAPSULATED_COMMAND:
|
||||
|
@ -367,14 +393,14 @@ static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
|
|||
case CDC_CLEAR_COMM_FEATURE:
|
||||
break;
|
||||
case CDC_SET_LINE_CODING:
|
||||
_cdc_set_line_coding(device, setup);
|
||||
vcom_connected = RT_TRUE;
|
||||
_cdc_set_line_coding(func->device, setup);
|
||||
data->connected = RT_TRUE;
|
||||
break;
|
||||
case CDC_GET_LINE_CODING:
|
||||
_cdc_get_line_coding(device, setup);
|
||||
_cdc_get_line_coding(func->device, setup);
|
||||
break;
|
||||
case CDC_SET_CONTROL_LINE_STATE:
|
||||
dcd_send_status(device->dcd);
|
||||
dcd_ep0_send_status(func->device->dcd);
|
||||
break;
|
||||
case CDC_SEND_BREAK:
|
||||
break;
|
||||
|
@ -387,92 +413,66 @@ static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
|
|||
}
|
||||
|
||||
/**
|
||||
* This function will run cdc class, it will be called on handle set configuration request.
|
||||
* This function will run cdc function, it will be called on handle set configuration request.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
* @param func the usb function object.
|
||||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
static rt_err_t _class_run(udevice_t device, uclass_t cls)
|
||||
static rt_err_t _function_enable(ufunction_t func)
|
||||
{
|
||||
cdc_eps_t eps;
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct vcom *data;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class run\n"));
|
||||
eps = (cdc_eps_t)cls->eps;
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
|
||||
eps->ep_in->buffer = tx_buf;
|
||||
eps->ep_out->buffer = rx_buf;
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
|
||||
|
||||
_vcom_reset_state();
|
||||
_vcom_reset_state(func);
|
||||
|
||||
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
|
||||
eps->ep_out->ep_desc->wMaxPacketSize);
|
||||
data = (struct vcom*)func->user_data;
|
||||
data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
|
||||
|
||||
data->ep_out->request.buffer = data->ep_out->buffer;
|
||||
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
|
||||
|
||||
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
|
||||
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will stop cdc class, it will be called on handle set configuration request.
|
||||
* This function will stop cdc function, it will be called on handle set configuration request.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
* @param func the usb function object.
|
||||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
static rt_err_t _class_stop(udevice_t device, uclass_t cls)
|
||||
static rt_err_t _function_disable(ufunction_t func)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct vcom *data;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class stop\n"));
|
||||
RT_ASSERT(func != RT_NULL);
|
||||
|
||||
_vcom_reset_state();
|
||||
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
|
||||
|
||||
_vcom_reset_state(func);
|
||||
|
||||
data = (struct vcom*)func->user_data;
|
||||
if(data->ep_out->buffer != RT_NULL)
|
||||
{
|
||||
rt_free(data->ep_out->buffer);
|
||||
data->ep_out->buffer = RT_NULL;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will handle system sof event.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
|
||||
static struct ufunction_ops ops =
|
||||
{
|
||||
rt_uint32_t level;
|
||||
rt_size_t size;
|
||||
cdc_eps_t eps;
|
||||
|
||||
if (vcom_connected != RT_TRUE)
|
||||
return -RT_ERROR;
|
||||
|
||||
if (vcom_in_sending)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
eps = (cdc_eps_t)cls->eps;
|
||||
|
||||
size = rt_ringbuffer_data_len(&tx_ringbuffer);
|
||||
if (size == 0)
|
||||
return -RT_EFULL;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, size);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* send data to host */
|
||||
vcom_in_sending = RT_TRUE;
|
||||
dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, size);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static struct uclass_ops ops =
|
||||
{
|
||||
_class_run,
|
||||
_class_stop,
|
||||
_class_sof_handler,
|
||||
_function_enable,
|
||||
_function_disable,
|
||||
RT_NULL,
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -483,7 +483,8 @@ static struct uclass_ops ops =
|
|||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm, rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
|
||||
static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm,
|
||||
rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
|
||||
{
|
||||
comm->call_mgmt_desc.data_interface = dintf_nr;
|
||||
comm->union_desc.master_interface = cintf_nr;
|
||||
|
@ -496,16 +497,16 @@ static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm, rt_uint8_t cintf_n
|
|||
}
|
||||
|
||||
/**
|
||||
* This function will create a cdc class instance.
|
||||
* This function will create a cdc function instance.
|
||||
*
|
||||
* @param device the usb device object.
|
||||
*
|
||||
* @return RT_EOK on successful.
|
||||
*/
|
||||
uclass_t rt_usbd_class_cdc_create(udevice_t device)
|
||||
ufunction_t rt_usbd_function_cdc_create(udevice_t device)
|
||||
{
|
||||
uclass_t cdc;
|
||||
cdc_eps_t eps;
|
||||
ufunction_t func;
|
||||
struct vcom* data;
|
||||
uintf_t intf_comm, intf_data;
|
||||
ualtsetting_t comm_setting, data_setting;
|
||||
ucdc_data_desc_t data_desc;
|
||||
|
@ -516,19 +517,26 @@ uclass_t rt_usbd_class_cdc_create(udevice_t device)
|
|||
|
||||
/* set usb device string description */
|
||||
rt_usbd_device_set_string(device, _ustring);
|
||||
/* create a cdc class */
|
||||
cdc = rt_usbd_class_create(device, &dev_desc, &ops);
|
||||
/* create a cdc class endpoints collection */
|
||||
eps = rt_malloc(sizeof(struct cdc_eps));
|
||||
cdc->eps = (void*)eps;
|
||||
|
||||
/* create a cdc function */
|
||||
func = rt_usbd_function_new(device, &dev_desc, &ops);
|
||||
rt_usbd_device_set_qualifier(device, &dev_qualifier);
|
||||
|
||||
/* allocate memory for cdc vcom data */
|
||||
data = (struct vcom*)rt_malloc(sizeof(struct vcom));
|
||||
rt_memset(data, 0, sizeof(struct vcom));
|
||||
func->user_data = (void*)data;
|
||||
|
||||
/* initilize vcom */
|
||||
rt_usb_vcom_init(func);
|
||||
|
||||
/* create a cdc communication interface and a cdc data interface */
|
||||
intf_comm = rt_usbd_interface_create(device, _interface_handler);
|
||||
intf_data = rt_usbd_interface_create(device, _interface_handler);
|
||||
intf_comm = rt_usbd_interface_new(device, _interface_handler);
|
||||
intf_data = rt_usbd_interface_new(device, _interface_handler);
|
||||
|
||||
/* create a communication alternate setting and a data alternate setting */
|
||||
comm_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_comm_descriptor));
|
||||
data_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_data_descriptor));
|
||||
comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_comm_descriptor));
|
||||
data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
|
||||
|
||||
/* config desc in alternate setting */
|
||||
rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
|
||||
|
@ -537,39 +545,39 @@ uclass_t rt_usbd_class_cdc_create(udevice_t device)
|
|||
/* configure the cdc interface descriptor */
|
||||
_cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);
|
||||
|
||||
/* create a bulk in and a bulk endpoint */
|
||||
data_desc = (ucdc_data_desc_t)data_setting->desc;
|
||||
eps->ep_out = rt_usbd_endpoint_create(&data_desc->ep_out_desc, _ep_out_handler);
|
||||
eps->ep_in = rt_usbd_endpoint_create(&data_desc->ep_in_desc, _ep_in_handler);
|
||||
|
||||
/* add the bulk out and bulk in endpoints to the data alternate setting */
|
||||
rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
|
||||
rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
|
||||
|
||||
/* add the data alternate setting to the data interface
|
||||
then set default setting of the interface */
|
||||
rt_usbd_interface_add_altsetting(intf_data, data_setting);
|
||||
rt_usbd_set_altsetting(intf_data, 0);
|
||||
|
||||
/* add the cdc data interface to cdc class */
|
||||
rt_usbd_class_add_interface(cdc, intf_data);
|
||||
|
||||
/* create a command endpoint */
|
||||
comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
|
||||
eps->ep_cmd = rt_usbd_endpoint_create(&comm_desc->ep_desc, _ep_cmd_handler);
|
||||
data->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
|
||||
|
||||
/* add the command endpoint to the cdc communication interface */
|
||||
rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
|
||||
rt_usbd_altsetting_add_endpoint(comm_setting, data->ep_cmd);
|
||||
|
||||
/* add the communication alternate setting to the communication interface,
|
||||
then set default setting of the interface */
|
||||
rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
|
||||
rt_usbd_set_altsetting(intf_comm, 0);
|
||||
|
||||
/* add the communication interface to the cdc class */
|
||||
rt_usbd_class_add_interface(cdc, intf_comm);
|
||||
/* add the communication interface to the cdc function */
|
||||
rt_usbd_function_add_interface(func, intf_comm);
|
||||
|
||||
return cdc;
|
||||
/* create a bulk in and a bulk endpoint */
|
||||
data_desc = (ucdc_data_desc_t)data_setting->desc;
|
||||
data->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
|
||||
data->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
|
||||
|
||||
/* add the bulk out and bulk in endpoints to the data alternate setting */
|
||||
rt_usbd_altsetting_add_endpoint(data_setting, data->ep_in);
|
||||
rt_usbd_altsetting_add_endpoint(data_setting, data->ep_out);
|
||||
|
||||
/* add the data alternate setting to the data interface
|
||||
then set default setting of the interface */
|
||||
rt_usbd_interface_add_altsetting(intf_data, data_setting);
|
||||
rt_usbd_set_altsetting(intf_data, 0);
|
||||
|
||||
/* add the cdc data interface to cdc function */
|
||||
rt_usbd_function_add_interface(func, intf_data);
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -597,80 +605,106 @@ static rt_err_t _vcom_control(struct rt_serial_device *serial,
|
|||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int _vcom_putc(struct rt_serial_device *serial, char c)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
int cnt;
|
||||
|
||||
if (vcom_connected != RT_TRUE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* if the buffer is full, there is a chance that the host would pull some
|
||||
* data out soon. But we cannot rely on that and if we wait to long, just
|
||||
* return. */
|
||||
for (cnt = 500;
|
||||
rt_ringbuffer_space_len(&tx_ringbuffer) == 0 && cnt;
|
||||
cnt--)
|
||||
{
|
||||
/*rt_kprintf("wait for %d\n", cnt);*/
|
||||
if (vcom_connected != RT_TRUE)
|
||||
return 0;
|
||||
}
|
||||
if (cnt == 0)
|
||||
{
|
||||
/* OK, we believe that the connection is lost. So don't send any more
|
||||
* data and act as the USB cable is not plugged in. Reset the VCOM
|
||||
* state machine */
|
||||
_vcom_reset_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (rt_ringbuffer_space_len(&tx_ringbuffer))
|
||||
{
|
||||
rt_ringbuffer_putchar(&tx_ringbuffer, c);
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _vcom_getc(struct rt_serial_device *serial)
|
||||
{
|
||||
int result;
|
||||
rt_uint8_t ch;
|
||||
rt_uint32_t level;
|
||||
struct ufunction *func;
|
||||
struct vcom *data;
|
||||
|
||||
func = (struct ufunction*)serial->parent.user_data;
|
||||
data = (struct vcom*)func->user_data;
|
||||
|
||||
result = -1;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (rt_ringbuffer_data_len(&rx_ringbuffer))
|
||||
|
||||
if(rt_ringbuffer_getchar(&data->rx_ringbuffer, &ch) != 0)
|
||||
{
|
||||
rt_ringbuffer_getchar(&rx_ringbuffer, &ch);
|
||||
result = ch;
|
||||
}
|
||||
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static rt_size_t _vcom_tx(struct rt_serial_device *serial,
|
||||
const char *buf, rt_size_t size)
|
||||
{
|
||||
struct vcom_tx_msg msg;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
RT_ASSERT(buf != RT_NULL);
|
||||
|
||||
msg.buf = buf;
|
||||
msg.serial = serial;
|
||||
msg.size = size;
|
||||
|
||||
if (rt_mq_send(&vcom_tx_thread_mq, (void*)&msg,
|
||||
sizeof(struct vcom_tx_msg)) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("vcom send msg fail\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static const struct rt_uart_ops usb_vcom_ops =
|
||||
{
|
||||
_vcom_configure,
|
||||
_vcom_control,
|
||||
_vcom_putc,
|
||||
RT_NULL,
|
||||
_vcom_getc,
|
||||
RT_NULL,
|
||||
//_vcom_tx,
|
||||
};
|
||||
|
||||
void rt_usb_vcom_init(void)
|
||||
/* Vcom Tx Thread */
|
||||
static void vcom_tx_thread_entry(void* parameter)
|
||||
{
|
||||
struct vcom_tx_msg msg;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (rt_mq_recv(&vcom_tx_thread_mq, (void*)&msg,
|
||||
sizeof(struct vcom_tx_msg), RT_WAITING_FOREVER) == RT_EOK)
|
||||
{
|
||||
struct ufunction *func;
|
||||
struct vcom *data;
|
||||
|
||||
func = (struct ufunction*)msg.serial->parent.user_data;
|
||||
data = (struct vcom*)func->user_data;
|
||||
if (!data->connected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
rt_completion_init(&data->wait);
|
||||
|
||||
data->ep_in->request.buffer = (void*)msg.buf;
|
||||
data->ep_in->request.size = msg.size;
|
||||
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
||||
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
||||
|
||||
if (rt_completion_wait(&data->wait, TX_TIMEOUT) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("vcom tx timeout\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void rt_usb_vcom_init(struct ufunction *func)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
struct serial_configure config;
|
||||
struct vcom *data = (struct vcom*)func->user_data;
|
||||
|
||||
/* initialize ring buffer */
|
||||
rt_ringbuffer_init(&rx_ringbuffer, rx_rbp, CDC_RX_BUFSIZE);
|
||||
rt_ringbuffer_init(&tx_ringbuffer, tx_rbp, CDC_TX_BUFSIZE);
|
||||
rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE);
|
||||
|
||||
config.baud_rate = BAUD_RATE_115200;
|
||||
config.bit_order = BIT_ORDER_LSB;
|
||||
|
@ -679,14 +713,25 @@ void rt_usb_vcom_init(void)
|
|||
config.stop_bits = STOP_BITS_1;
|
||||
config.invert = NRZ_NORMAL;
|
||||
|
||||
vcom_serial.ops = &usb_vcom_ops;
|
||||
vcom_serial.int_rx = &vcom_int_rx;
|
||||
vcom_serial.config = config;
|
||||
data->serial.ops = &usb_vcom_ops;
|
||||
data->serial.int_rx = &data->vcom_int_rx;
|
||||
data->serial.config = config;
|
||||
|
||||
/* register vcom device */
|
||||
rt_hw_serial_register(&vcom_serial, "vcom",
|
||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
||||
RT_NULL);
|
||||
rt_hw_serial_register(&data->serial, VCOM_DEVICE,
|
||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
|
||||
func);
|
||||
|
||||
/* create an vcom message queue */
|
||||
rt_mq_init(&vcom_tx_thread_mq, "vcomq", vcom_tx_thread_mq_pool, VCOM_MQ_MSG_SZ,
|
||||
sizeof(vcom_tx_thread_mq_pool), RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* init usb device thread */
|
||||
rt_thread_init(&vcom_thread, "vcom", vcom_tx_thread_entry, RT_NULL,
|
||||
vcom_thread_stack, 512, 8, 20);
|
||||
result = rt_thread_startup(&vcom_thread);
|
||||
RT_ASSERT(result == RT_EOK);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -59,13 +59,6 @@ struct request_sense_data
|
|||
rt_uint8_t Reserved4[4];
|
||||
}request_sense_data_t;
|
||||
|
||||
struct mass_eps
|
||||
{
|
||||
uep_t ep_in;
|
||||
uep_t ep_out;
|
||||
};
|
||||
typedef struct mass_eps* mass_eps_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
#endif
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -28,13 +28,15 @@
|
|||
|
||||
#ifdef RT_USING_USB_DEVICE
|
||||
|
||||
#define USB_DEVICE_CONTROLLER_NAME "usbd"
|
||||
|
||||
#ifdef RT_USB_DEVICE_COMPOSITE
|
||||
const static char* ustring[] =
|
||||
{
|
||||
"Language",
|
||||
"RT-Thread Team.",
|
||||
"RTT Composite Device",
|
||||
"1.1.0",
|
||||
"320219198301",
|
||||
"Configuration",
|
||||
"Interface",
|
||||
};
|
||||
|
@ -60,54 +62,54 @@ static struct udevice_descriptor compsit_desc =
|
|||
};
|
||||
#endif
|
||||
|
||||
rt_err_t rt_usb_device_init(const char* udc_name)
|
||||
rt_err_t rt_usb_device_init(void)
|
||||
{
|
||||
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;
|
||||
}
|
||||
ufunction_t func;
|
||||
|
||||
/* create and startup usb device thread */
|
||||
rt_usbd_core_init();
|
||||
|
||||
/* create a device object */
|
||||
udevice = rt_usbd_device_create();
|
||||
udevice = rt_usbd_device_new();
|
||||
|
||||
udc = rt_device_find(USB_DEVICE_CONTROLLER_NAME);
|
||||
if(udc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find usb device controller %s\n", USB_DEVICE_CONTROLLER_NAME);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* 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();
|
||||
cfg = rt_usbd_config_new();
|
||||
|
||||
#ifdef RT_USB_DEVICE_MSTORAGE
|
||||
/* create a mass storage class object */
|
||||
cls = rt_usbd_class_mstorage_create(udevice);
|
||||
/* create a mass storage function object */
|
||||
func = rt_usbd_function_mstorage_create(udevice);
|
||||
|
||||
/* add the class to the configuration */
|
||||
rt_usbd_config_add_class(cfg, cls);
|
||||
/* add the function to the configuration */
|
||||
rt_usbd_config_add_function(cfg, func);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USB_DEVICE_CDC
|
||||
/* create a cdc class object */
|
||||
cls = rt_usbd_class_cdc_create(udevice);
|
||||
/* create a cdc function object */
|
||||
func = rt_usbd_function_cdc_create(udevice);
|
||||
|
||||
/* add the class to the configuration */
|
||||
rt_usbd_config_add_class(cfg, cls);
|
||||
/* add the function to the configuration */
|
||||
rt_usbd_config_add_function(cfg, func);
|
||||
#endif
|
||||
#ifdef RT_USB_DEVICE_RNDIS
|
||||
/* create a rndis class object */
|
||||
cls = rt_usbd_class_rndis_create(udevice);
|
||||
|
||||
/* add the class to the configuration */
|
||||
rt_usbd_config_add_class(cfg, cls);
|
||||
#ifdef RT_USB_DEVICE_RNDIS
|
||||
/* create a rndis function object */
|
||||
func = rt_usbd_function_rndis_create(udevice);
|
||||
|
||||
/* add the function to the configuration */
|
||||
rt_usbd_config_add_function(cfg, func);
|
||||
#endif
|
||||
|
||||
/* set device descriptor to the device */
|
||||
|
@ -115,18 +117,18 @@ rt_err_t rt_usb_device_init(const char* udc_name)
|
|||
rt_usbd_device_set_descriptor(udevice, &compsit_desc);
|
||||
rt_usbd_device_set_string(udevice, ustring);
|
||||
#else
|
||||
rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
|
||||
rt_usbd_device_set_descriptor(udevice, func->dev_desc);
|
||||
#endif
|
||||
|
||||
/* 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);
|
||||
|
||||
/* set default configuration to 1 */
|
||||
rt_usbd_set_config(udevice, 1);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue