[usb]update usb stack

This commit is contained in:
tangyuxin
2017-11-11 10:51:47 +08:00
parent f7a6078c5c
commit cc54e0a74e
21 changed files with 2744 additions and 261 deletions

View File

@@ -27,7 +27,8 @@
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "drivers/usb_common.h"
#include "drivers/usb_device.h"
static rt_list_t device_list;
@@ -37,10 +38,10 @@ static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep);
static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);
/**
* This function will handle get_device_descriptor request.
* This function will handle get_device_descriptor bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -54,22 +55,21 @@ static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
/* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
USB_DESC_LENGTH_DEVICE : setup->length;
/* device descriptor wLength should less than USB_DESC_LENGTH_DEVICE*/
size = (setup->wLength > USB_DESC_LENGTH_DEVICE) ?
USB_DESC_LENGTH_DEVICE : setup->wLength;
/* send device descriptor to endpoint 0 */
rt_usbd_ep0_write(device, (rt_uint8_t*)&device->dev_desc,
size);
rt_usbd_ep0_write(device, (rt_uint8_t*) &device->dev_desc, size);
return RT_EOK;
}
/**
* This function will handle get_config_descriptor request.
* This function will handle get_config_descriptor bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -85,8 +85,8 @@ static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
cfg_desc = &device->curr_cfg->cfg_desc;
size = (setup->length > cfg_desc->wTotalLength) ?
cfg_desc->wTotalLength : setup->length;
size = (setup->wLength > cfg_desc->wTotalLength) ?
cfg_desc->wTotalLength : setup->wLength;
/* send configuration descriptor to endpoint 0 */
rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size);
@@ -95,12 +95,12 @@ static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
}
/**
* This function will handle get_string_descriptor request.
* This function will handle get_string_descriptor bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful, -RT_ERROR on invalid request.
* @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
*/
static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
{
@@ -115,7 +115,7 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
str_desc.type = USB_DESC_TYPE_STRING;
index = setup->value & 0xFF;
index = setup->wValue & 0xFF;
if(index > USB_STRING_INTERFACE_INDEX)
{
@@ -141,10 +141,10 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
}
}
if (setup->length > str_desc.bLength)
if (setup->wLength > str_desc.bLength)
len = str_desc.bLength;
else
len = setup->length;
len = setup->wLength;
/* send string descriptor to endpoint 0 */
rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len);
@@ -175,10 +175,10 @@ static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup)
}
/**
* This function will handle get_descriptor request.
* This function will handle get_descriptor bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -190,7 +190,7 @@ static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
if(setup->request_type == USB_REQ_TYPE_DIR_IN)
{
switch(setup->value >> 8)
switch(setup->wValue >> 8)
{
case USB_DESC_TYPE_DEVICE:
_get_device_descriptor(device, setup);
@@ -220,10 +220,10 @@ static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
}
/**
* This function will handle get_interface request.
* This function will handle get_interface bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -245,7 +245,7 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
}
/* find the specified interface and its alternate setting */
intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
value = intf->curr_setting->intf_desc->bAlternateSetting;
/* send the interface alternate setting to endpoint 0*/
@@ -255,10 +255,10 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
}
/**
* This function will handle set_interface request.
* This function will handle set_interface bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -282,10 +282,10 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
}
/* find the specified interface */
intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
/* set alternate setting to the interface */
rt_usbd_set_altsetting(intf, setup->value & 0xFF);
rt_usbd_set_altsetting(intf, setup->wValue & 0xFF);
setting = intf->curr_setting;
/* start all endpoints of the interface alternate setting */
@@ -301,10 +301,10 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
}
/**
* This function will handle get_config request.
* This function will handle get_config bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -335,10 +335,10 @@ static rt_err_t _get_config(struct udevice* device, ureq_t setup)
}
/**
* This function will handle set_config request.
* This function will handle set_config bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -356,13 +356,13 @@ static rt_err_t _set_config(struct udevice* device, ureq_t setup)
RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
if (setup->value > device->dev_desc.bNumConfigurations)
if (setup->wValue > device->dev_desc.bNumConfigurations)
{
rt_usbd_ep0_set_stall(device);
return -RT_ERROR;
}
if (setup->value == 0)
if (setup->wValue == 0)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
device->state = USB_STATE_ADDRESS;
@@ -371,8 +371,7 @@ static rt_err_t _set_config(struct udevice* device, ureq_t setup)
}
/* set current configuration */
rt_usbd_set_config(device, setup->value);
dcd_set_config(device->dcd, setup->value);
rt_usbd_set_config(device, setup->wValue);
cfg = device->curr_cfg;
for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
@@ -406,10 +405,10 @@ _exit:
}
/**
* This function will handle set_address request.
* This function will handle set_address bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -419,12 +418,12 @@ static rt_err_t _set_address(struct udevice* device, ureq_t setup)
RT_ASSERT(device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
/* set address in device control driver */
dcd_set_address(device->dcd, setup->wValue);
/* issue status stage */
dcd_ep0_send_status(device->dcd);
/* set address in device control driver */
dcd_set_address(device->dcd, setup->value);
RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
device->state = USB_STATE_ADDRESS;
@@ -433,11 +432,11 @@ static rt_err_t _set_address(struct udevice* device, ureq_t setup)
}
/**
* This function will handle standard request to
* This function will handle standard bRequest to
* interface that defined in function-specifics
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -453,7 +452,7 @@ static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func);
intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
if (intf != RT_NULL)
{
ret = intf->handler(func, setup);
@@ -467,10 +466,10 @@ static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
}
/**
* This function will handle standard request.
* This function will handle standard bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
@@ -488,17 +487,17 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
{
case USB_REQ_TYPE_DEVICE:
switch(setup->request)
switch(setup->bRequest)
{
case USB_REQ_GET_STATUS:
rt_usbd_ep0_write(device, &value, 2);
break;
case USB_REQ_CLEAR_FEATURE:
rt_usbd_clear_feature(device, setup->value, setup->index);
rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
dcd_ep0_send_status(dcd);
break;
case USB_REQ_SET_FEATURE:
rt_usbd_set_feature(device, setup->value, setup->index);
rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
break;
case USB_REQ_SET_ADDRESS:
_set_address(device, setup);
@@ -522,7 +521,7 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
}
break;
case USB_REQ_TYPE_INTERFACE:
switch(setup->request)
switch(setup->bRequest)
{
case USB_REQ_GET_INTERFACE:
_get_interface(device, setup);
@@ -542,13 +541,13 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
}
break;
case USB_REQ_TYPE_ENDPOINT:
switch(setup->request)
switch(setup->bRequest)
{
case USB_REQ_GET_STATUS:
{
uep_t ep;
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
value = ep->stalled;
rt_usbd_ep0_write(device, &value, 2);
}
@@ -559,10 +558,10 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
uio_request_t req;
struct rt_list_node *node;
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
if(USB_EP_HALT == setup->value && ep->stalled == RT_TRUE)
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
if(USB_EP_HALT == setup->wValue && ep->stalled == RT_TRUE)
{
rt_usbd_clear_feature(device, setup->value, setup->index);
rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
dcd_ep0_send_status(dcd);
ep->stalled = RT_FALSE;
@@ -581,11 +580,11 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
{
uep_t ep;
if(USB_EP_HALT == setup->value)
if(USB_EP_HALT == setup->wValue)
{
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
ep->stalled = RT_TRUE;
rt_usbd_set_feature(device, setup->value, setup->index);
rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
dcd_ep0_send_status(dcd);
}
}
@@ -612,12 +611,12 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
}
/**
* This function will handle function request.
* This function will handle function bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful, -RT_ERROR on invalid request.
* @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
*/
static rt_err_t _function_request(udevice_t device, ureq_t setup)
{
@@ -628,8 +627,8 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup)
RT_ASSERT(device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
/* verify request value */
if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
/* verify bRequest wValue */
if(setup->wIndex > device->curr_cfg->cfg_desc.bNumInterfaces)
{
rt_usbd_ep0_set_stall(device);
return -RT_ERROR;
@@ -638,7 +637,7 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup)
switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
{
case USB_REQ_TYPE_INTERFACE:
intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func);
intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
if(intf == RT_NULL)
{
rt_kprintf("unkwown interface request\n");
@@ -663,24 +662,24 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup)
static rt_err_t _dump_setup_packet(ureq_t setup)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request 0x%x\n",
RT_DEBUG_LOG(RT_DEBUG_USB, (" setup_request : 0x%x\n",
setup->request_type));
RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
RT_DEBUG_LOG(RT_DEBUG_USB, (" value : 0x%x\n", setup->wValue));
RT_DEBUG_LOG(RT_DEBUG_USB, (" length : 0x%x\n", setup->wLength));
RT_DEBUG_LOG(RT_DEBUG_USB, (" index : 0x%x\n", setup->wIndex));
RT_DEBUG_LOG(RT_DEBUG_USB, (" request : 0x%x\n", setup->bRequest));
RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
return RT_EOK;
}
/**
* This function will handle setup request.
* This function will handle setup bRequest.
*
* @param device the usb device object.
* @param setup the setup request.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful, -RT_ERROR on invalid request.
* @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
*/
static rt_err_t _setup_request(udevice_t device, ureq_t setup)
{
@@ -741,17 +740,16 @@ static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg)
if(EP_ADDRESS(ep) & USB_DIR_IN)
{
size = ep_msg->size;
if(ep->request.remain_size >= EP_MAXPACKET(ep))
{
dcd_ep_write(device->dcd, EP_ADDRESS(ep),
ep->request.buffer, EP_MAXPACKET(ep));
dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, EP_MAXPACKET(ep));
ep->request.remain_size -= EP_MAXPACKET(ep);
ep->request.buffer += EP_MAXPACKET(ep);
}
else if(ep->request.remain_size > 0)
{
dcd_ep_write(device->dcd, EP_ADDRESS(ep),
ep->request.buffer, ep->request.remain_size);
dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size);
ep->request.remain_size = 0;
}
else
@@ -769,21 +767,12 @@ static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg)
if(size == 0)
{
size = dcd_ep_read(device->dcd, EP_ADDRESS(ep),
ep->request.buffer);
size = dcd_ep_read(device->dcd, EP_ADDRESS(ep), ep->request.buffer);
}
ep->request.remain_size -= size;
ep->request.buffer += size;
if(size > ep->request.remain_size)
{
ep->request.remain_size = 0;
}
else
{
ep->request.remain_size -= size;
ep->request.buffer += size;
}
if(ep->request.req_type == UIO_REQUEST_READ_MOST)
if(ep->request.req_type == UIO_REQUEST_READ_BEST)
{
EP_HANDLER(ep, func, size);
}
@@ -807,6 +796,7 @@ static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg)
ep0 = &device->dcd->ep0;
size = ep_msg->size;
if(ep0->request.remain_size == 0)
{
return RT_EOK;
@@ -889,7 +879,6 @@ static rt_err_t _stop_notify(udevice_t device)
static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
{
rt_uint16_t maxpacket;
rt_size_t sent_size;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->dcd != RT_NULL);
@@ -898,18 +887,18 @@ static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_s
maxpacket = EP_MAXPACKET(ep);
if(ep->request.remain_size >= maxpacket)
{
sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
ep->request.remain_size -= sent_size;
dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
ep->request.remain_size -= maxpacket;
ep->request.buffer += maxpacket;
}
else
{
sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer,
dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer,
ep->request.remain_size);
ep->request.remain_size -= sent_size;
ep->request.remain_size = 0;
}
return sent_size;
return size;
}
static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
@@ -1047,7 +1036,7 @@ uconfig_t rt_usbd_config_new(void)
}
rt_memset(cfg, 0, sizeof(struct uconfig));
/* set default value */
/* set default wValue */
cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
@@ -1258,7 +1247,7 @@ udevice_t rt_usbd_find_device(udcd_t dcd)
* This function will find an usb configuration object.
*
* @param device the usb device object.
* @param value the configuration number.
* @param wValue the configuration number.
*
* @return an usb configuration object on found or RT_NULL on not found.
*/
@@ -1291,7 +1280,7 @@ uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
* This function will find an usb interface object.
*
* @param device the usb device object.
* @param value the interface number.
* @param wValue the interface number.
*
* @return an usb configuration object on found or RT_NULL on not found.
*/
@@ -1332,7 +1321,7 @@ uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *
* This function will find an usb interface alternate setting object.
*
* @param device the usb device object.
* @param value the alternate setting number.
* @param wValue the alternate setting number.
*
* @return an usb interface alternate setting object on found or RT_NULL on not found.
*/
@@ -1348,7 +1337,7 @@ ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
if(intf->curr_setting != RT_NULL)
{
/* if the value equal to the current alternate setting, then do not search */
/* if the wValue equal to the current alternate setting, then do not search */
if(intf->curr_setting->intf_desc->bAlternateSetting == value)
return intf->curr_setting;
}
@@ -1384,8 +1373,7 @@ uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_
RT_ASSERT(device != RT_NULL);
/* search a endpoint in the current configuration */
for (i=device->curr_cfg->func_list.next;
i!=&device->curr_cfg->func_list; i=i->next)
for (i=device->curr_cfg->func_list.next; i!=&device->curr_cfg->func_list; i=i->next)
{
func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
@@ -1563,7 +1551,7 @@ rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
* This function will set an alternate setting for an interface.
*
* @param intf_desc the interface descriptor.
* @param value the alternate setting number.
* @param wValue the alternate setting number.
*
* @return RT_EOK.
*/
@@ -1589,7 +1577,7 @@ rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
* This function will set a configuration for an usb device.
*
* @param device the usb device object.
* @param value the configuration number.
* @param wValue the configuration number.
*
* @return RT_EOK.
*/
@@ -1608,16 +1596,18 @@ rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
/* set as current configuration */
device->curr_cfg = cfg;
dcd_set_config(device->dcd, value);
return RT_TRUE;
}
/**
* This function will request an IO transaction.
* This function will bRequest an IO transaction.
*
* @param device the usb device object.
* @param ep the endpoint object.
* @param req IO request.
* @param req IO bRequest.
*
* @return RT_EOK.
*/
@@ -1632,7 +1622,7 @@ rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
{
switch(req->req_type)
{
case UIO_REQUEST_READ_MOST:
case UIO_REQUEST_READ_BEST:
case UIO_REQUEST_READ_FULL:
ep->request.remain_size = ep->request.size;
size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size);
@@ -1659,7 +1649,7 @@ rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
* This function will set feature for an usb device.
*
* @param device the usb device object.
* @param value the configuration number.
* @param wValue the configuration number.
*
* @return RT_EOK.
*/
@@ -1684,7 +1674,7 @@ rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t in
* This function will clear feature for an usb device.
*
* @param device the usb device object.
* @param value the configuration number.
* @param wValue the configuration number.
*
* @return RT_EOK.
*/
@@ -1766,8 +1756,7 @@ static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep)
while(device->dcd->ep_pool[i].addr != 0xFF)
{
if(device->dcd->ep_pool[i].status == ID_UNASSIGNED &&
ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type &&
ep->ep_desc->bEndpointAddress == device->dcd->ep_pool[i].dir)
ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type)
{
EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr;
ep->id = &device->dcd->ep_pool[i];
@@ -1832,6 +1821,7 @@ rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
{
dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, dcd->ep0.id->maxpacket);
dcd->ep0.request.remain_size -= dcd->ep0.id->maxpacket;
dcd->ep0.request.buffer += dcd->ep0.id->maxpacket;
}
else if(dcd->ep0.request.remain_size > 0)
{
@@ -1860,7 +1850,7 @@ rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
return RT_EOK;
}
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address)
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
{
struct udev_msg msg;
@@ -1869,7 +1859,7 @@ rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address)
msg.type = USB_MSG_DATA_NOTIFY;
msg.dcd = dcd;
msg.content.ep_msg.ep_addr = address;
msg.content.ep_msg.size = 0;
msg.content.ep_msg.size = size;
rt_usbd_event_signal(&msg);
return RT_EOK;
@@ -1945,7 +1935,6 @@ rt_err_t rt_usbd_sof_handler(udcd_t dcd)
rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
{
uep_t ep0;
rt_size_t sent_size = 0;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->dcd != RT_NULL);
@@ -1956,19 +1945,20 @@ rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
ep0->request.size = size;
ep0->request.buffer = buffer;
ep0->request.remain_size = size;
if(ep0->request.remain_size >= ep0->id->maxpacket)
{
sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->id->maxpacket);
ep0->request.remain_size -= sent_size;
dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->id->maxpacket);
ep0->request.remain_size -= ep0->id->maxpacket;
ep0->request.buffer += ep0->id->maxpacket;
}
else
{
sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->request.remain_size);
ep0->request.remain_size -= sent_size;
dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->request.remain_size);
ep0->request.remain_size = 0;
}
return sent_size;
return size;
}
rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
@@ -2094,12 +2084,18 @@ rt_err_t rt_usbd_core_init(void)
rt_list_init(&device_list);
/* create an usb message queue */
rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ,
sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO);
rt_mq_init(&usb_mq,
"usbd",
usb_mq_pool, USBD_MQ_MSG_SZ,
sizeof(usb_mq_pool),
RT_IPC_FLAG_FIFO);
/* init usb device thread */
rt_thread_init(&usb_thread, "usbd", rt_usbd_thread_entry, RT_NULL,
usb_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
rt_thread_init(&usb_thread,
"usbd",
rt_usbd_thread_entry, RT_NULL,
usb_thread_stack, RT_USBD_THREAD_STACK_SZ,
RT_USBD_THREAD_PRIO, 20);
/* rt_thread_init should always be OK, so start the thread without further
* checking. */
return rt_thread_startup(&usb_thread);

View File

@@ -89,27 +89,47 @@ rt_err_t rt_usb_device_init(void)
cfg = rt_usbd_config_new();
#ifdef RT_USB_DEVICE_MSTORAGE
/* create a mass storage function object */
func = rt_usbd_function_mstorage_create(udevice);
{
extern ufunction_t rt_usbd_function_mstorage_create(udevice_t device);
/* create a mass storage function object */
func = rt_usbd_function_mstorage_create(udevice);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
}
#endif
#ifdef RT_USB_DEVICE_CDC
/* create a cdc function object */
func = rt_usbd_function_cdc_create(udevice);
{
extern ufunction_t rt_usbd_function_cdc_create(udevice_t device);
/* create a cdc function object */
func = rt_usbd_function_cdc_create(udevice);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
}
#endif
#ifdef RT_USB_DEVICE_HID
{
extern ufunction_t rt_usbd_function_hid_create(udevice_t device);
/* create a cdc function object */
func = rt_usbd_function_hid_create(udevice);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
}
#endif
#ifdef RT_USB_DEVICE_RNDIS
/* create a rndis function object */
func = rt_usbd_function_rndis_create(udevice);
{
extern ufunction_t rt_usbd_function_rndis_create(udevice_t device);
/* 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);
/* add the function to the configuration */
rt_usbd_config_add_function(cfg, func);
}
#endif
/* set device descriptor to the device */
@@ -131,5 +151,4 @@ rt_err_t rt_usb_device_init(void)
return RT_EOK;
}
#endif