提交 18d42fe0 编写于 作者: lymzzyh's avatar lymzzyh

[Components][USBHOST]Core OK

上级 b42fc964
...@@ -49,8 +49,9 @@ extern "C" { ...@@ -49,8 +49,9 @@ extern "C" {
#define USBH_PID_DATA 0x01 #define USBH_PID_DATA 0x01
struct uhcd; struct uhcd;
struct uintf; struct uhintf;
struct uhub; struct uhub;
struct upipe;
struct uclass_driver struct uclass_driver
{ {
...@@ -83,8 +84,9 @@ struct uinstance ...@@ -83,8 +84,9 @@ struct uinstance
ucfg_desc_t cfg_desc; ucfg_desc_t cfg_desc;
struct uhcd *hcd; struct uhcd *hcd;
upipe_t pipe_ep0_out; struct upipe * pipe_ep0_out;
upipe_t pipe_ep0_in; struct upipe * pipe_ep0_in;
rt_list_t pipe;
rt_uint8_t status; rt_uint8_t status;
rt_uint8_t type; rt_uint8_t type;
...@@ -110,10 +112,11 @@ struct uhintf ...@@ -110,10 +112,11 @@ struct uhintf
struct upipe struct upipe
{ {
rt_list_t list;
rt_uint8_t pipe_index; rt_uint8_t pipe_index;
rt_uint32_t status; rt_uint32_t status;
struct uendpoint_descriptor ep; struct uendpoint_descriptor ep;
struct uhintf* intf; uinst_t inst;
func_callback callback; func_callback callback;
void* user_data; void* user_data;
}; };
...@@ -127,7 +130,7 @@ struct uhub ...@@ -127,7 +130,7 @@ struct uhub
struct uinstance* child[USB_HUB_PORT_NUM]; struct uinstance* child[USB_HUB_PORT_NUM];
rt_bool_t is_roothub; rt_bool_t is_roothub;
upipe_t pipe_in;
rt_uint8_t buffer[8]; rt_uint8_t buffer[8];
struct uinstance* self; struct uinstance* self;
struct uhcd *hcd; struct uhcd *hcd;
...@@ -136,15 +139,17 @@ typedef struct uhub* uhub_t; ...@@ -136,15 +139,17 @@ typedef struct uhub* uhub_t;
struct uhcd_ops struct uhcd_ops
{ {
rt_err_t (*reset_port) (rt_uint8_t port);
int (*pipe_xfer) (upipe_t pipe, rt_uint8_t token, void* buffer, int nbytes, int timeout); int (*pipe_xfer) (upipe_t pipe, rt_uint8_t token, void* buffer, int nbytes, int timeout);
rt_err_t (*alloc_pipe) (struct upipe* pipe, uep_desc_t ep); rt_err_t (*open_pipe) (upipe_t pipe);
rt_err_t (*free_pipe) (upipe_t pipe); rt_err_t (*close_pipe) (upipe_t pipe);
}; };
typedef struct uhcd_ops* uhcd_ops_t; typedef struct uhcd_ops* uhcd_ops_t;
struct uhcd struct uhcd
{ {
struct rt_device parent; struct rt_device parent;
uhcd_ops_t ops; uhcd_ops_t ops;
rt_uint8_t num_ports;
uhub_t roothub; uhub_t roothub;
}; };
typedef struct uhcd* uhcd_t; typedef struct uhcd* uhcd_t;
...@@ -173,10 +178,10 @@ typedef struct uhost_msg* uhost_msg_t; ...@@ -173,10 +178,10 @@ typedef struct uhost_msg* uhost_msg_t;
/* usb host system interface */ /* usb host system interface */
rt_err_t rt_usb_host_init(void); rt_err_t rt_usb_host_init(void);
void rt_usbh_hub_init(void); void rt_usbh_hub_init(struct uhcd *hcd);
/* usb host core interface */ /* usb host core interface */
struct uinstance* rt_usbh_alloc_instance(void); struct uinstance* rt_usbh_alloc_instance(uhcd_t uhcd);
rt_err_t rt_usbh_attatch_instance(struct uinstance* device); rt_err_t rt_usbh_attatch_instance(struct uinstance* device);
rt_err_t rt_usbh_detach_instance(struct uinstance* device); rt_err_t rt_usbh_detach_instance(struct uinstance* device);
rt_err_t rt_usbh_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer, int nbytes); rt_err_t rt_usbh_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer, int nbytes);
...@@ -204,9 +209,9 @@ ucd_t rt_usbh_class_driver_storage(void); ...@@ -204,9 +209,9 @@ ucd_t rt_usbh_class_driver_storage(void);
/* usb hub interface */ /* usb hub interface */
rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
rt_size_t size); rt_size_t size);
rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer); rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer);
rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port, rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port,
rt_uint8_t* buffer); rt_uint32_t* buffer);
rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port, rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port,
rt_uint16_t feature); rt_uint16_t feature);
rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port, rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
...@@ -214,22 +219,56 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port, ...@@ -214,22 +219,56 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port); rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port);
rt_err_t rt_usbh_event_signal(struct uhost_msg* msg); rt_err_t rt_usbh_event_signal(struct uhost_msg* msg);
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port);
/* usb host controller driver interface */ /* usb host controller driver interface */
rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t pipe, uep_desc_t ep) rt_inline rt_err_t rt_usb_instance_add_pipe(uinst_t inst, upipe_t pipe)
{
RT_ASSERT(inst != RT_NULL);
RT_ASSERT(pipe != RT_NULL);
rt_list_insert_before(&inst->pipe, &pipe->list);
return RT_EOK;
}
rt_inline upipe_t rt_usb_instance_find_pipe(uinst_t inst,rt_uint8_t ep_address)
{
rt_list_t * l;
for(l = inst->pipe.next;l != &inst->pipe;l = l->next)
{
if(rt_list_entry(l,struct upipe,list)->ep.bEndpointAddress == ep_address)
{
return rt_list_entry(l,struct upipe,list);
}
}
return RT_NULL;
}
rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe, uinst_t inst, uep_desc_t ep)
{ {
return hcd->ops->alloc_pipe(pipe, ep); *pipe = (upipe_t)rt_malloc(sizeof(struct upipe));
if(*pipe == RT_NULL)
{
return RT_ERROR;
}
rt_memset(*pipe,0,sizeof(struct upipe));
(*pipe)->inst = inst;
rt_memcpy(&(*pipe)->ep,ep,sizeof(struct uendpoint_descriptor));
return hcd->ops->open_pipe(*pipe);
}
rt_inline void rt_usb_pipe_add_callback(upipe_t pipe, func_callback callback)
{
pipe->callback = callback;
} }
rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe) rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
{ {
RT_ASSERT(pipe != RT_NULL); RT_ASSERT(pipe != RT_NULL);
return hcd->ops->free_pipe(pipe); hcd->ops->close_pipe(pipe);
rt_free(pipe);
return RT_EOK;
} }
rt_inline int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout) int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout);
{
return hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, buffer, nbytes, timeout);
}
rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int timeout) rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int timeout)
{ {
return hcd->ops->pipe_xfer(pipe, USBH_PID_SETUP, (void *)setup, 8, timeout); return hcd->ops->pipe_xfer(pipe, USBH_PID_SETUP, (void *)setup, 8, timeout);
...@@ -241,3 +280,4 @@ rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int ...@@ -241,3 +280,4 @@ rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int
#endif #endif
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
#ifdef RT_USBH_MSTORAGE #ifdef RT_USBH_MSTORAGE
extern rt_err_t rt_udisk_run(struct uintf* intf); extern rt_err_t rt_udisk_run(struct uhintf* intf);
extern rt_err_t rt_udisk_stop(struct uintf* intf); extern rt_err_t rt_udisk_stop(struct uhintf* intf);
static struct uclass_driver storage_driver; static struct uclass_driver storage_driver;
...@@ -31,7 +31,7 @@ static struct uclass_driver storage_driver; ...@@ -31,7 +31,7 @@ static struct uclass_driver storage_driver;
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t _pipe_check(struct uintf* intf, upipe_t pipe) static rt_err_t _pipe_check(struct uhintf* intf, upipe_t pipe)
{ {
struct uinstance* device; struct uinstance* device;
rt_err_t ret; rt_err_t ret;
...@@ -74,7 +74,7 @@ static rt_err_t _pipe_check(struct uintf* intf, upipe_t pipe) ...@@ -74,7 +74,7 @@ static rt_err_t _pipe_check(struct uintf* intf, upipe_t pipe)
RT_DEBUG_LOG(RT_DEBUG_USB, ("clean storage in pipe stall\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("clean storage in pipe stall\n"));
/* it should receive csw after clear the stall feature */ /* it should receive csw after clear the stall feature */
size = rt_usb_hcd_bulk_xfer(stor->pipe_in->intf->device->hcd, size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd,
stor->pipe_in, &csw, SIZEOF_CSW, 100); stor->pipe_in, &csw, SIZEOF_CSW, 100);
if(size != SIZEOF_CSW) if(size != SIZEOF_CSW)
{ {
...@@ -93,7 +93,7 @@ static rt_err_t _pipe_check(struct uintf* intf, upipe_t pipe) ...@@ -93,7 +93,7 @@ static rt_err_t _pipe_check(struct uintf* intf, upipe_t pipe)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, static rt_err_t rt_usb_bulk_only_xfer(struct uhintf* intf,
ustorage_cbw_t cmd, rt_uint8_t* buffer, int timeout) ustorage_cbw_t cmd, rt_uint8_t* buffer, int timeout)
{ {
rt_size_t size; rt_size_t size;
...@@ -116,7 +116,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, ...@@ -116,7 +116,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf,
do do
{ {
/* send the cbw */ /* send the cbw */
size = rt_usb_hcd_bulk_xfer(intf->device->hcd, stor->pipe_out, size = rt_usb_hcd_pipe_xfer(stor->pipe_out->inst->hcd, stor->pipe_out,
cmd, SIZEOF_CBW, timeout); cmd, SIZEOF_CBW, timeout);
if(size != SIZEOF_CBW) if(size != SIZEOF_CBW)
{ {
...@@ -127,7 +127,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, ...@@ -127,7 +127,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf,
{ {
pipe = (cmd->dflags == CBWFLAGS_DIR_IN) ? stor->pipe_in : pipe = (cmd->dflags == CBWFLAGS_DIR_IN) ? stor->pipe_in :
stor->pipe_out; stor->pipe_out;
size = rt_usb_hcd_bulk_xfer(intf->device->hcd, pipe, (void*)buffer, size = rt_usb_hcd_pipe_xfer(pipe->inst->hcd, pipe, (void*)buffer,
cmd->xfer_len, timeout); cmd->xfer_len, timeout);
if(size != cmd->xfer_len) if(size != cmd->xfer_len)
{ {
...@@ -138,7 +138,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, ...@@ -138,7 +138,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf,
} }
/* receive the csw */ /* receive the csw */
size = rt_usb_hcd_bulk_xfer(intf->device->hcd, stor->pipe_in, size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd, stor->pipe_in,
&csw, SIZEOF_CSW, timeout); &csw, SIZEOF_CSW, timeout);
if(size != SIZEOF_CSW) if(size != SIZEOF_CSW)
{ {
...@@ -172,7 +172,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, ...@@ -172,7 +172,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf,
if(csw.status != 0) if(csw.status != 0)
{ {
rt_kprintf("csw status error\n"); rt_kprintf("csw status error:%d\n",csw.status);
return -RT_ERROR; return -RT_ERROR;
} }
...@@ -187,7 +187,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf, ...@@ -187,7 +187,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uintf* intf,
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun) rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun)
{ {
struct uinstance* device; struct uinstance* device;
struct urequest setup; struct urequest setup;
...@@ -209,15 +209,20 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun) ...@@ -209,15 +209,20 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun)
/* construct the request */ /* construct the request */
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_INTERFACE; USB_REQ_TYPE_INTERFACE;
setup.request = USBREQ_GET_MAX_LUN; setup.bRequest = USBREQ_GET_MAX_LUN;
setup.index = intf->intf_desc->bInterfaceNumber; setup.wValue = intf->intf_desc->bInterfaceNumber;
setup.length = 1; setup.wIndex = 1;
setup.value = 0; setup.wLength = 0;
/* do control transfer request */ /* do control transfer request */
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, max_lun, 1, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 1) return -RT_EIO; {
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, max_lun, 1, timeout) != 1)
{
return -RT_EIO;
}
return -RT_EIO;
}
return RT_EOK; return RT_EOK;
} }
...@@ -228,7 +233,7 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun) ...@@ -228,7 +233,7 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_reset(struct uintf* intf) rt_err_t rt_usbh_storage_reset(struct uhintf* intf)
{ {
struct urequest setup; struct urequest setup;
struct uinstance* device; struct uinstance* device;
...@@ -250,13 +255,15 @@ rt_err_t rt_usbh_storage_reset(struct uintf* intf) ...@@ -250,13 +255,15 @@ rt_err_t rt_usbh_storage_reset(struct uintf* intf)
/* construct the request */ /* construct the request */
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_INTERFACE; USB_REQ_TYPE_INTERFACE;
setup.request = USBREQ_MASS_STORAGE_RESET; setup.bRequest = USBREQ_MASS_STORAGE_RESET;
setup.index = intf->intf_desc->bInterfaceNumber; setup.wIndex = intf->intf_desc->bInterfaceNumber;
setup.length = 0; setup.wLength = 0;
setup.value = 0; setup.wValue = 0;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 0) return -RT_EIO; {
return -RT_EIO;
}
return RT_EOK; return RT_EOK;
} }
...@@ -271,7 +278,7 @@ rt_err_t rt_usbh_storage_reset(struct uintf* intf) ...@@ -271,7 +278,7 @@ rt_err_t rt_usbh_storage_reset(struct uintf* intf)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_read10(struct uintf* intf, rt_uint8_t *buffer, rt_err_t rt_usbh_storage_read10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout) rt_uint32_t sector, rt_size_t count, int timeout)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
...@@ -317,7 +324,7 @@ rt_err_t rt_usbh_storage_read10(struct uintf* intf, rt_uint8_t *buffer, ...@@ -317,7 +324,7 @@ rt_err_t rt_usbh_storage_read10(struct uintf* intf, rt_uint8_t *buffer,
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_write10(struct uintf* intf, rt_uint8_t *buffer, rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout) rt_uint32_t sector, rt_size_t count, int timeout)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
...@@ -361,7 +368,7 @@ rt_err_t rt_usbh_storage_write10(struct uintf* intf, rt_uint8_t *buffer, ...@@ -361,7 +368,7 @@ rt_err_t rt_usbh_storage_write10(struct uintf* intf, rt_uint8_t *buffer,
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_request_sense(struct uintf* intf, rt_uint8_t* buffer) rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
int timeout = 200; int timeout = 200;
...@@ -397,7 +404,7 @@ rt_err_t rt_usbh_storage_request_sense(struct uintf* intf, rt_uint8_t* buffer) ...@@ -397,7 +404,7 @@ rt_err_t rt_usbh_storage_request_sense(struct uintf* intf, rt_uint8_t* buffer)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_test_unit_ready(struct uintf* intf) rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
int timeout = 200; int timeout = 200;
...@@ -433,7 +440,7 @@ rt_err_t rt_usbh_storage_test_unit_ready(struct uintf* intf) ...@@ -433,7 +440,7 @@ rt_err_t rt_usbh_storage_test_unit_ready(struct uintf* intf)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_inquiry(struct uintf* intf, rt_uint8_t* buffer) rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
int timeout = 200; int timeout = 200;
...@@ -470,7 +477,7 @@ rt_err_t rt_usbh_storage_inquiry(struct uintf* intf, rt_uint8_t* buffer) ...@@ -470,7 +477,7 @@ rt_err_t rt_usbh_storage_inquiry(struct uintf* intf, rt_uint8_t* buffer)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_usbh_storage_get_capacity(struct uintf* intf, rt_uint8_t* buffer) rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer)
{ {
struct ustorage_cbw cmd; struct ustorage_cbw cmd;
int timeout = 200; int timeout = 200;
...@@ -512,7 +519,7 @@ static rt_err_t rt_usbh_storage_enable(void* arg) ...@@ -512,7 +519,7 @@ static rt_err_t rt_usbh_storage_enable(void* arg)
int i = 0; int i = 0;
rt_err_t ret; rt_err_t ret;
ustor_t stor; ustor_t stor;
struct uintf* intf = (struct uintf*)arg; struct uhintf* intf = (struct uhintf*)arg;
/* parameter check */ /* parameter check */
if(intf == RT_NULL) if(intf == RT_NULL)
...@@ -556,16 +563,12 @@ static rt_err_t rt_usbh_storage_enable(void* arg) ...@@ -556,16 +563,12 @@ static rt_err_t rt_usbh_storage_enable(void* arg)
if(ep_desc->bEndpointAddress & USB_DIR_IN) if(ep_desc->bEndpointAddress & USB_DIR_IN)
{ {
/* alloc an in pipe for the storage instance */ /* alloc an in pipe for the storage instance */
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &stor->pipe_in, stor->pipe_in = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
intf, ep_desc, RT_NULL);
if(ret != RT_EOK) return ret;
} }
else else
{ {
/* alloc an output pipe for the storage instance */ /* alloc an output pipe for the storage instance */
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &stor->pipe_out, stor->pipe_out = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
intf, ep_desc, RT_NULL);
if(ret != RT_EOK) return ret;
} }
} }
...@@ -594,7 +597,7 @@ static rt_err_t rt_usbh_storage_enable(void* arg) ...@@ -594,7 +597,7 @@ static rt_err_t rt_usbh_storage_enable(void* arg)
static rt_err_t rt_usbh_storage_disable(void* arg) static rt_err_t rt_usbh_storage_disable(void* arg)
{ {
ustor_t stor; ustor_t stor;
struct uintf* intf = (struct uintf*)arg; struct uhintf* intf = (struct uhintf*)arg;
/* parameter check */ /* parameter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
...@@ -608,16 +611,6 @@ static rt_err_t rt_usbh_storage_disable(void* arg) ...@@ -608,16 +611,6 @@ static rt_err_t rt_usbh_storage_disable(void* arg)
rt_udisk_stop(intf); rt_udisk_stop(intf);
rt_kprintf("in 0x%x, out 0x%x\n", stor->pipe_in,
stor->pipe_out);
/* free in pipe */
if(stor->pipe_in != RT_NULL)
rt_usb_hcd_free_pipe(intf->device->hcd, stor->pipe_in);
/* free out pipe */
if(stor->pipe_out != RT_NULL)
rt_usb_hcd_free_pipe(intf->device->hcd, stor->pipe_out);
/* free storage instance */ /* free storage instance */
if(stor != RT_NULL) rt_free(stor); if(stor != RT_NULL) rt_free(stor);
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
struct ustor_data struct ustor_data
{ {
struct dfs_partition part; struct dfs_partition part;
struct uintf* intf; struct uhintf* intf;
int udisk_id; int udisk_id;
const char path; const char path;
}; };
...@@ -40,15 +40,15 @@ struct ustor ...@@ -40,15 +40,15 @@ struct ustor
}; };
typedef struct ustor* ustor_t; typedef struct ustor* ustor_t;
rt_err_t rt_usbh_storage_get_max_lun(struct uintf* intf, rt_uint8_t* max_lun); rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun);
rt_err_t rt_usbh_storage_reset(struct uintf* intf); rt_err_t rt_usbh_storage_reset(struct uhintf* intf);
rt_err_t rt_usbh_storage_read10(struct uintf* intf, rt_uint8_t *buffer, rt_err_t rt_usbh_storage_read10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout); rt_uint32_t sector, rt_size_t count, int timeout);
rt_err_t rt_usbh_storage_write10(struct uintf* intf, rt_uint8_t *buffer, rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout); rt_uint32_t sector, rt_size_t count, int timeout);
rt_err_t rt_usbh_storage_request_sense(struct uintf* intf, rt_uint8_t* buffer); rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer);
rt_err_t rt_usbh_storage_test_unit_ready(struct uintf* intf); rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf);
rt_err_t rt_usbh_storage_inquiry(struct uintf* intf, rt_uint8_t* buffer); rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer);
rt_err_t rt_usbh_storage_get_capacity(struct uintf* intf, rt_uint8_t* buffer); rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer);
#endif #endif
...@@ -72,7 +72,7 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, ...@@ -72,7 +72,7 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
rt_size_t size) rt_size_t size)
{ {
rt_err_t ret; rt_err_t ret;
struct uintf* intf; struct uhintf* intf;
struct ustor_data* data; struct ustor_data* data;
int timeout = 500; int timeout = 500;
...@@ -110,7 +110,7 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff ...@@ -110,7 +110,7 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff
rt_size_t size) rt_size_t size)
{ {
rt_err_t ret; rt_err_t ret;
struct uintf* intf; struct uhintf* intf;
struct ustor_data* data; struct ustor_data* data;
int timeout = 500; int timeout = 500;
...@@ -174,7 +174,7 @@ static rt_err_t rt_udisk_control(rt_device_t dev, rt_uint8_t cmd, void *args) ...@@ -174,7 +174,7 @@ static rt_err_t rt_udisk_control(rt_device_t dev, rt_uint8_t cmd, void *args)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_udisk_run(struct uintf* intf) rt_err_t rt_udisk_run(struct uhintf* intf)
{ {
int i = 0; int i = 0;
rt_err_t ret; rt_err_t ret;
...@@ -382,7 +382,7 @@ rt_err_t rt_udisk_run(struct uintf* intf) ...@@ -382,7 +382,7 @@ rt_err_t rt_udisk_run(struct uintf* intf)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_udisk_stop(struct uintf* intf) rt_err_t rt_udisk_stop(struct uhintf* intf)
{ {
int i; int i;
ustor_t stor; ustor_t stor;
......
...@@ -35,7 +35,7 @@ static struct uinstance dev[USB_MAX_DEVICE]; ...@@ -35,7 +35,7 @@ static struct uinstance dev[USB_MAX_DEVICE];
* *
* @return the allocate instance on successful, or RT_NULL on failure. * @return the allocate instance on successful, or RT_NULL on failure.
*/ */
uinst_t rt_usbh_alloc_instance(void) uinst_t rt_usbh_alloc_instance(uhcd_t uhcd)
{ {
int i; int i;
...@@ -54,7 +54,8 @@ uinst_t rt_usbh_alloc_instance(void) ...@@ -54,7 +54,8 @@ uinst_t rt_usbh_alloc_instance(void)
dev[i].index = i + 1; dev[i].index = i + 1;
dev[i].address = 0; dev[i].address = 0;
dev[i].max_packet_size = 0x8; dev[i].max_packet_size = 0x8;
rt_list_init(&dev[i].pipe);
dev[i].hcd = uhcd;
/* unlock scheduler */ /* unlock scheduler */
rt_exit_critical(); rt_exit_critical();
return &dev[i]; return &dev[i];
...@@ -75,6 +76,26 @@ uinst_t rt_usbh_alloc_instance(void) ...@@ -75,6 +76,26 @@ uinst_t rt_usbh_alloc_instance(void)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static struct uendpoint_descriptor ep0_out_desc =
{
/*endpoint descriptor*/
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
0x00 | USB_DIR_OUT,
USB_EP_ATTR_CONTROL,
0x40,
0x00,
};
static struct uendpoint_descriptor ep0_in_desc =
{
/*endpoint descriptor*/
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
0x00 | USB_DIR_IN,
USB_EP_ATTR_CONTROL,
0x40,
0x00,
};
rt_err_t rt_usbh_attatch_instance(uinst_t device) rt_err_t rt_usbh_attatch_instance(uinst_t device)
{ {
int i = 0; int i = 0;
...@@ -82,12 +103,18 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -82,12 +103,18 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
struct uconfig_descriptor cfg_desc; struct uconfig_descriptor cfg_desc;
udev_desc_t dev_desc; udev_desc_t dev_desc;
uintf_desc_t intf_desc; uintf_desc_t intf_desc;
uep_desc_t ep_desc;
rt_uint8_t ep_index;
upipe_t pipe;
ucd_t drv; ucd_t drv;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor)); rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor));
dev_desc = &device->dev_desc; dev_desc = &device->dev_desc;
/* alloc address 0 ep0 pipe*/
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n"));
...@@ -98,19 +125,25 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -98,19 +125,25 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
rt_kprintf("get device descriptor head failed\n"); rt_kprintf("get device descriptor head failed\n");
return ret; return ret;
} }
//should reset bus
rt_usbh_hub_reset_port(device->parent_hub, device->port);
/* set device address */ /* set device address */
ret = rt_usbh_set_address(device); ret = rt_usbh_set_address(device);
/* free address 0 ep0 pipe*/
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
if(ret != RT_EOK) if(ret != RT_EOK)
{ {
rt_kprintf("set device address failed\n"); rt_kprintf("set device address failed\n");
return ret; return ret;
} }
/* free true address ep0 pipe*/
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
/* set device max packet size */ /* set device max packet size */
device->max_packet_size = device->dev_desc.bMaxPacketSize0; device->max_packet_size = device->dev_desc.bMaxPacketSize0;
RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n", RT_DEBUG_LOG(1, ("get device descriptor length %d\n",
dev_desc->bLength)); dev_desc->bLength));
/* get full device descriptor again */ /* get full device descriptor again */
...@@ -122,8 +155,8 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -122,8 +155,8 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
return ret; return ret;
} }
RT_DEBUG_LOG(RT_DEBUG_USB, ("Vendor ID 0x%x\n", dev_desc->idVendor)); RT_DEBUG_LOG(1, ("Vendor ID 0x%x\n", dev_desc->idVendor));
RT_DEBUG_LOG(RT_DEBUG_USB, ("Product ID 0x%x\n", dev_desc->idProduct)); RT_DEBUG_LOG(1, ("Product ID 0x%x\n", dev_desc->idProduct));
/* get configuration descriptor head */ /* get configuration descriptor head */
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18); ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18);
...@@ -148,8 +181,10 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -148,8 +181,10 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
/* set configuration */ /* set configuration */
ret = rt_usbh_set_configure(device, 1); ret = rt_usbh_set_configure(device, 1);
if(ret != RT_EOK) return ret; if(ret != RT_EOK)
{
return ret;
}
for(i=0; i<device->cfg_desc->bNumInterfaces; i++) for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
{ {
/* get interface descriptor through configuration descriptor */ /* get interface descriptor through configuration descriptor */
...@@ -160,10 +195,28 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -160,10 +195,28 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
return -RT_ERROR; return -RT_ERROR;
} }
RT_DEBUG_LOG(RT_DEBUG_USB, ("interface class 0x%x, subclass 0x%x\n", RT_DEBUG_LOG(1, ("interface class 0x%x, subclass 0x%x\n",
intf_desc->bInterfaceClass, intf_desc->bInterfaceClass,
intf_desc->bInterfaceSubClass)); intf_desc->bInterfaceSubClass));
/* alloc pipe*/
for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++)
{
rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
if(ep_desc != RT_NULL)
{
if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
{
rt_kprintf("alloc pipe failed\n");
return RT_ERROR;
}
rt_usb_instance_add_pipe(device,pipe);
}
else
{
rt_kprintf("get endpoint desc failed\n");
return RT_ERROR;
}
}
/* find driver by class code found in interface descriptor */ /* find driver by class code found in interface descriptor */
drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass, drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass,
intf_desc->bInterfaceSubClass); intf_desc->bInterfaceSubClass);
...@@ -171,9 +224,7 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -171,9 +224,7 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
if(drv != RT_NULL) if(drv != RT_NULL)
{ {
/* allocate memory for interface device */ /* allocate memory for interface device */
device->intf[i] = device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
(struct uintf*)rt_malloc(sizeof(struct uintf));
device->intf[i]->drv = drv; device->intf[i]->drv = drv;
device->intf[i]->device = device; device->intf[i]->device = device;
device->intf[i]->intf_desc = intf_desc; device->intf[i]->intf_desc = intf_desc;
...@@ -207,7 +258,7 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) ...@@ -207,7 +258,7 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device)
rt_err_t rt_usbh_detach_instance(uinst_t device) rt_err_t rt_usbh_detach_instance(uinst_t device)
{ {
int i = 0; int i = 0;
rt_list_t * l;
if(device == RT_NULL) if(device == RT_NULL)
{ {
rt_kprintf("no usb instance to detach\n"); rt_kprintf("no usb instance to detach\n");
...@@ -215,8 +266,6 @@ rt_err_t rt_usbh_detach_instance(uinst_t device) ...@@ -215,8 +266,6 @@ rt_err_t rt_usbh_detach_instance(uinst_t device)
} }
/* free configration descriptor */ /* free configration descriptor */
if(device->cfg_desc) rt_free(device->cfg_desc);
for(i=0; i<device->cfg_desc->bNumInterfaces; i++) for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
{ {
if(device->intf[i] == RT_NULL) continue; if(device->intf[i] == RT_NULL) continue;
...@@ -227,7 +276,15 @@ rt_err_t rt_usbh_detach_instance(uinst_t device) ...@@ -227,7 +276,15 @@ rt_err_t rt_usbh_detach_instance(uinst_t device)
RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i)); RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]); rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
} }
if(device->cfg_desc) rt_free(device->cfg_desc);
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
for(l = device->pipe.next;l != &device->pipe;l = l->next)
{
rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list));
}
rt_memset(device, 0, sizeof(struct uinstance)); rt_memset(device, 0, sizeof(struct uinstance));
return RT_EOK; return RT_EOK;
...@@ -258,9 +315,17 @@ rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer, ...@@ -258,9 +315,17 @@ rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
setup.wLength = nbytes; setup.wLength = nbytes;
setup.wValue = type << 8; setup.wValue = type << 8;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, nbytes, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
timeout) != nbytes) return -RT_EIO; {
else return RT_EOK; if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
{
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
{
return RT_EOK;
}
}
}
return RT_ERROR;
} }
/** /**
...@@ -286,13 +351,15 @@ rt_err_t rt_usbh_set_address(uinst_t device) ...@@ -286,13 +351,15 @@ rt_err_t rt_usbh_set_address(uinst_t device)
setup.wLength = 0; setup.wLength = 0;
setup.wValue = device->index; setup.wValue = device->index;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 0) return -RT_EIO; {
return RT_ERROR;
rt_thread_delay(50); }
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
{
device->address = device->index;
}
device->address = device->index;
return RT_EOK; return RT_EOK;
} }
...@@ -319,8 +386,10 @@ rt_err_t rt_usbh_set_configure(uinst_t device, int config) ...@@ -319,8 +386,10 @@ rt_err_t rt_usbh_set_configure(uinst_t device, int config)
setup.wLength = 0; setup.wLength = 0;
setup.wValue = config; setup.wValue = config;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 0) return -RT_EIO; {
return RT_ERROR;
}
return RT_EOK; return RT_EOK;
} }
...@@ -348,8 +417,10 @@ rt_err_t rt_usbh_set_interface(uinst_t device, int intf) ...@@ -348,8 +417,10 @@ rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
setup.wLength = 0; setup.wLength = 0;
setup.wValue = intf; setup.wValue = intf;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 0) return -RT_EIO; {
return RT_ERROR;
}
return RT_EOK; return RT_EOK;
} }
...@@ -377,8 +448,10 @@ rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature) ...@@ -377,8 +448,10 @@ rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
setup.wLength = 0; setup.wLength = 0;
setup.wValue = feature; setup.wValue = feature;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
timeout) != 0) return -RT_EIO; {
return RT_ERROR;
}
return RT_EOK; return RT_EOK;
} }
...@@ -447,6 +520,7 @@ rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, ...@@ -447,6 +520,7 @@ rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
/* check parameter */ /* check parameter */
RT_ASSERT(intf_desc != RT_NULL); RT_ASSERT(intf_desc != RT_NULL);
RT_ASSERT(num < intf_desc->bNumEndpoints); RT_ASSERT(num < intf_desc->bNumEndpoints);
*ep_desc = RT_NULL;
ptr = (rt_uint32_t)intf_desc + intf_desc->bLength; ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
while(count < intf_desc->bNumEndpoints) while(count < intf_desc->bNumEndpoints)
...@@ -476,3 +550,22 @@ rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, ...@@ -476,3 +550,22 @@ rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
return -RT_EIO; return -RT_EIO;
} }
int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout)
{
rt_size_t remain_size;
rt_size_t send_size;
remain_size = nbytes;
do
{
send_size = (remain_size > pipe->ep.wMaxPacketSize) ? pipe->ep.wMaxPacketSize : remain_size;
if(hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, buffer, send_size, timeout) == send_size)
{
remain_size -= send_size;
}
else
{
return 0;
}
}while(remain_size > 0);
return nbytes;
}
...@@ -29,6 +29,95 @@ ...@@ -29,6 +29,95 @@
static struct rt_messagequeue *usb_mq; static struct rt_messagequeue *usb_mq;
static struct uclass_driver hub_driver; static struct uclass_driver hub_driver;
static struct uhub root_hub;
static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args)
{
switch(cmd)
{
case RH_GET_PORT_STATUS:
(*(rt_uint32_t *)args) = hcd->roothub->port_status[port-1];
break;
case RH_SET_PORT_STATUS:
hcd->roothub->port_status[port-1] = (*(rt_uint32_t *)args);
break;
case RH_CLEAR_PORT_FEATURE:
switch(((rt_uint32_t)args))
{
case PORT_FEAT_C_CONNECTION:
hcd->roothub->port_status[port-1] &= ~PORT_CCSC;
break;
case PORT_FEAT_C_ENABLE:
hcd->roothub->port_status[port-1] &= ~PORT_PESC;
break;
case PORT_FEAT_C_SUSPEND:
hcd->roothub->port_status[port-1] &= ~PORT_PSSC;
break;
case PORT_FEAT_C_OVER_CURRENT:
hcd->roothub->port_status[port-1] &= ~PORT_POCIC;
break;
case PORT_FEAT_C_RESET:
hcd->roothub->port_status[port-1] &= ~PORT_PRSC;
break;
}
break;
case RH_SET_PORT_FEATURE:
switch((rt_uint32_t)args)
{
case PORT_FEAT_CONNECTION:
hcd->roothub->port_status[port-1] |= PORT_CCSC;
break;
case PORT_FEAT_ENABLE:
hcd->roothub->port_status[port-1] |= PORT_PESC;
break;
case PORT_FEAT_SUSPEND:
hcd->roothub->port_status[port-1] |= PORT_PSSC;
break;
case PORT_FEAT_OVER_CURRENT:
hcd->roothub->port_status[port-1] |= PORT_POCIC;
break;
case PORT_FEAT_RESET:
hcd->ops->reset_port(port);
break;
case PORT_FEAT_POWER:
break;
case PORT_FEAT_LOWSPEED:
break;
case PORT_FEAT_HIGHSPEED:
break;
}
break;
default:
return RT_ERROR;
}
return RT_EOK;
}
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
{
struct uhost_msg msg;
msg.type = USB_MSG_CONNECT_CHANGE;
msg.content.hub = hcd->roothub;
hcd->roothub->port_status[port - 1] |= PORT_CCS | PORT_CCSC;
if(isHS)
{
hcd->roothub->port_status[port - 1] &= ~PORT_LSDA;
}
else
{
hcd->roothub->port_status[port - 1] |= PORT_LSDA;
}
rt_usbh_event_signal(&msg);
}
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
{
struct uhost_msg msg;
msg.type = USB_MSG_CONNECT_CHANGE;
msg.content.hub = hcd->roothub;
hcd->roothub->port_status[port - 1] |= PORT_CCSC;
hcd->roothub->port_status[port - 1] &= ~PORT_CCS;
rt_usbh_event_signal(&msg);
}
/** /**
* This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance * This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
...@@ -117,7 +206,7 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* ...@@ -117,7 +206,7 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t*
/* get roothub port status */ /* get roothub port status */
if(hub->is_roothub) if(hub->is_roothub)
{ {
rt_usb_hcd_hub_control(hub->hcd, port, RH_GET_PORT_STATUS, root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
(void*)buffer); (void*)buffer);
return RT_EOK; return RT_EOK;
} }
...@@ -159,7 +248,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_ ...@@ -159,7 +248,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_
/* clear roothub feature */ /* clear roothub feature */
if(hub->is_roothub) if(hub->is_roothub)
{ {
rt_usb_hcd_hub_control(hub->hcd, port, RH_CLEAR_PORT_FEATURE, root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
(void*)feature); (void*)feature);
return RT_EOK; return RT_EOK;
} }
...@@ -200,7 +289,7 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port, ...@@ -200,7 +289,7 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
/* clear roothub feature */ /* clear roothub feature */
if(hub->is_roothub) if(hub->is_roothub)
{ {
rt_usb_hcd_hub_control(hub->hcd, port, RH_SET_PORT_FEATURE, root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
(void*)feature); (void*)feature);
return RT_EOK; return RT_EOK;
} }
...@@ -243,7 +332,7 @@ rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port) ...@@ -243,7 +332,7 @@ rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
while(1) while(1)
{ {
ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus); ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
if(!(pstatus & PORT_PRS)) break; if(!(pstatus & PORT_PRS)) break;
} }
...@@ -276,7 +365,7 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port) ...@@ -276,7 +365,7 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
for(i=0; i<times; i++) for(i=0; i<times; i++)
{ {
ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus); ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
if(!(pstatus & PORT_CCS)) if(!(pstatus & PORT_CCS))
...@@ -318,10 +407,10 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub) ...@@ -318,10 +407,10 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
reconnect = RT_FALSE; reconnect = RT_FALSE;
/* get hub port status */ /* get hub port status */
ret = rt_usbh_hub_get_port_status(hub, i + 1, (rt_uint8_t*)&pstatus); ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
if(ret != RT_EOK) continue; if(ret != RT_EOK) continue;
RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i, pstatus)); RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus));
/* check port status change */ /* check port status change */
if ((pstatus & PORT_CCSC)) if ((pstatus & PORT_CCSC))
...@@ -339,14 +428,14 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub) ...@@ -339,14 +428,14 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
if(reconnect) if(reconnect)
{ {
if(hub->child[i]->status != DEV_STATUS_IDLE) if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
rt_usbh_detach_instance(hub->child[i]); rt_usbh_detach_instance(hub->child[i]);
ret = rt_usbh_hub_port_debounce(hub, i + 1); ret = rt_usbh_hub_port_debounce(hub, i + 1);
if(ret != RT_EOK) continue; if(ret != RT_EOK) continue;
/* allocate an usb instance for new connected device */ /* allocate an usb instance for new connected device */
device = rt_usbh_alloc_instance(); device = rt_usbh_alloc_instance(hub->hcd);
if(device == RT_NULL) break; if(device == RT_NULL) break;
/* set usb device speed */ /* set usb device speed */
...@@ -375,31 +464,29 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub) ...@@ -375,31 +464,29 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
*/ */
static void rt_usbh_hub_irq(void* context) static void rt_usbh_hub_irq(void* context)
{ {
upipe_t pipe; upipe_t pipe;
struct uhintf* intf;
uhub_t hub; uhub_t hub;
int timeout = 100; int timeout = 100;
RT_ASSERT(context != RT_NULL); RT_ASSERT(context != RT_NULL);
pipe = (upipe_t)context; pipe = (upipe_t)context;
intf = pipe->intf; hub = (uhub_t)pipe->user_data;
hub = (uhub_t)intf->user_data;
if(pipe->status != UPIPE_STATUS_OK) if(pipe->status != UPIPE_STATUS_OK)
{ {
rt_kprintf("hub irq error\n"); RT_DEBUG_LOG(RT_DEBUG_USB,("hub irq error\n"));
return; return;
} }
rt_usbh_hub_port_change(hub); rt_usbh_hub_port_change(hub);
rt_kprintf("hub int xfer...\n"); rt_kprintf(RT_DEBUG_USB,("hub int xfer...\n"));
/* parameter check */ /* parameter check */
RT_ASSERT(pipe->intf->device->hcd != RT_NULL); RT_ASSERT(pipe->inst->hcd != RT_NULL);
rt_usb_hcd_pipe_xfer(intf->device->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout); rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
} }
/** /**
...@@ -410,6 +497,7 @@ static void rt_usbh_hub_irq(void* context) ...@@ -410,6 +497,7 @@ static void rt_usbh_hub_irq(void* context)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_hub_enable(void *arg) static rt_err_t rt_usbh_hub_enable(void *arg)
{ {
int i = 0; int i = 0;
...@@ -418,8 +506,8 @@ static rt_err_t rt_usbh_hub_enable(void *arg) ...@@ -418,8 +506,8 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
uhub_t hub; uhub_t hub;
struct uinstance* device; struct uinstance* device;
struct uhintf* intf = (struct uhintf*)arg; struct uhintf* intf = (struct uhintf*)arg;
upipe_t pipe_in;
int timeout = 300; int timeout = 300;
/* paremeter check */ /* paremeter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
...@@ -483,18 +571,21 @@ static rt_err_t rt_usbh_hub_enable(void *arg) ...@@ -483,18 +571,21 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
if(ep_desc->bEndpointAddress & USB_DIR_IN) if(ep_desc->bEndpointAddress & USB_DIR_IN)
{ {
/* allocate a pipe according to the endpoint type */ /* allocate a pipe according to the endpoint type */
rt_usb_hcd_alloc_pipe(device->hcd, &hub->pipe_in, intf, pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
ep_desc, rt_usbh_hub_irq); if(pipe_in == RT_NULL)
{
return RT_ERROR;
}
rt_usb_pipe_add_callback(pipe_in,rt_usbh_hub_irq);
} }
else return -RT_ERROR; else return -RT_ERROR;
} }
/* parameter check */ /* parameter check */
RT_ASSERT(device->hcd != RT_NULL); RT_ASSERT(device->hcd != RT_NULL);
pipe_in->user_data = hub;
rt_usb_hcd_int_xfer(device->hcd, hub->pipe_in, hub->buffer, rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
hub->pipe_in->ep.wMaxPacketSize, timeout); pipe_in->ep.wMaxPacketSize, timeout);
return RT_EOK; return RT_EOK;
} }
...@@ -510,20 +601,14 @@ static rt_err_t rt_usbh_hub_disable(void* arg) ...@@ -510,20 +601,14 @@ static rt_err_t rt_usbh_hub_disable(void* arg)
{ {
int i; int i;
uhub_t hub; uhub_t hub;
struct uinstance* device; struct uhintf* intf = (struct uhintf*)arg;
struct uintf* intf = (struct uintf*)arg;
/* paremeter check */ /* paremeter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
device = intf->device;
hub = (uhub_t)intf->user_data; hub = (uhub_t)intf->user_data;
if(hub->pipe_in != RT_NULL)
rt_usb_hcd_free_pipe(device->hcd, hub->pipe_in);
for(i=0; i<hub->num_ports; i++) for(i=0; i<hub->num_ports; i++)
{ {
if(hub->child[i] != RT_NULL) if(hub->child[i] != RT_NULL)
...@@ -610,10 +695,14 @@ rt_err_t rt_usbh_event_signal(struct uhost_msg* msg) ...@@ -610,10 +695,14 @@ rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
* @return none. * @return none.
* *
*/ */
void rt_usbh_hub_init(void) void rt_usbh_hub_init(uhcd_t hcd)
{ {
rt_thread_t thread; rt_thread_t thread;
/* link root hub to hcd */
root_hub.is_roothub = RT_TRUE;
hcd->roothub = &root_hub;
root_hub.hcd = hcd;
root_hub.num_ports = hcd->num_ports;
/* create usb message queue */ /* create usb message queue */
usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO); usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
......
...@@ -49,7 +49,7 @@ rt_err_t rt_usb_host_init(void) ...@@ -49,7 +49,7 @@ rt_err_t rt_usb_host_init(void)
} }
/* initialize usb hub */ /* initialize usb hub */
rt_usbh_hub_init(); rt_usbh_hub_init((uhcd_t)uhc);
/* initialize class driver */ /* initialize class driver */
rt_usbh_class_driver_init(); rt_usbh_class_driver_init();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册