浏览代码

1.replace API prefix usb* with usbh* in USB host stack to distinct with USB device stack;
2.adjust some viable and micro name
3.some other small fix

shaolin 11 年之前
父节点
当前提交
0fb9c54efd

+ 82 - 116
components/drivers/include/drivers/usb_host.h

@@ -37,17 +37,17 @@ extern "C" {
 #define USB_HUB_PORT_NUM                0x04
 #define SIZEOF_USB_REQUEST              0x08
 
-#define UINST_STATUS_IDLE               0x00
-#define UINST_STATUS_BUSY               0x01
-#define UINST_STATUS_ERROR              0x02
+#define DEV_STATUS_IDLE               0x00
+#define DEV_STATUS_BUSY               0x01
+#define DEV_STATUS_ERROR              0x02
 
 #define UPIPE_STATUS_OK                 0x00
 #define UPIPE_STATUS_STALL              0x01
 #define UPIPE_STATUS_ERROR              0x02
 
 struct uhcd;
-struct uifinst;
-struct uhubinst;
+struct uintf;
+struct uhub;
 
 struct uclass_driver
 {
@@ -55,8 +55,8 @@ struct uclass_driver
     int class_code;
     int subclass_code;
     
-    rt_err_t (*run)(void* arg);
-    rt_err_t (*stop)(void* arg);
+    rt_err_t (*enable)(void* arg);
+    rt_err_t (*disable)(void* arg);
     
     void* user_data;
 };
@@ -86,32 +86,31 @@ struct uinstance
     rt_uint8_t max_packet_size;    
     rt_uint8_t port;
 
-    struct uhubinst* parent;
-    struct uifinst* ifinst[USB_MAX_INTERFACE];        
+    struct uhub* parent;
+    struct uintf* intf[USB_MAX_INTERFACE];        
 };
 typedef struct uinstance* uinst_t;
 
-struct uifinst
+struct uintf
 {
-    uinst_t uinst;
+    struct uinstance* device;
     uintf_desc_t intf_desc;
 
     ucd_t drv;
     void *user_data;
 };
-typedef struct uifinst* uifinst_t;
 
 struct upipe
 {
     rt_uint32_t status;
     struct uendpoint_descriptor ep;
-    uifinst_t ifinst;
+    struct uintf* intf;
     func_callback callback;
     void* user_data;
 };
 typedef struct upipe* upipe_t;
 
-struct uhubinst
+struct uhub
 {
     struct uhub_descriptor hub_desc;
     rt_uint8_t num_ports;
@@ -122,19 +121,19 @@ struct uhubinst
     upipe_t pipe_in;
     rt_uint8_t buffer[8];    
     struct uinstance* self;
-    struct uhcd *hcd;    
+    struct uhcd *hcd;
 };    
-typedef struct uhubinst* uhubinst_t;
+typedef struct uhub* uhub_t;
 
 struct uhcd_ops
 {
-    int (*ctl_xfer)(uinst_t inst, ureq_t setup, void* buffer, int nbytes, 
+    int (*ctl_xfer)(struct uinstance* inst, ureq_t setup, void* buffer, int nbytes, 
         int timeout);
     int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
     int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
     int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
     
-    rt_err_t (*alloc_pipe)(struct upipe** pipe, uifinst_t ifinst, uep_desc_t ep, 
+    rt_err_t (*alloc_pipe)(struct upipe** pipe, struct uintf* intf, uep_desc_t ep, 
         func_callback callback);
     rt_err_t (*free_pipe)(upipe_t pipe);    
     rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);    
@@ -144,6 +143,7 @@ struct uhcd
 {
     struct rt_device parent;
     struct uhcd_ops* ops;
+    struct uhub* roothub; 
 };
 typedef struct uhcd* uhcd_t;
 
@@ -159,7 +159,7 @@ struct uhost_msg
     uhost_msg_type type; 
     union
     {
-        struct uhubinst* uhub;
+        struct uhub* hub;
         struct 
         {
             func_callback function;
@@ -170,82 +170,67 @@ struct uhost_msg
 typedef struct uhost_msg* uhost_msg_t;
 
 /* usb host system interface */
-void rt_usb_host_init(void);
-void rt_usb_hub_thread(void);
+rt_err_t rt_usb_host_init(void);
+void rt_usbh_hub_init(void);
 
 /* usb host core interface */
-uinst_t rt_usb_alloc_instance(void);
-rt_err_t rt_usb_attatch_instance(uinst_t uinst);
-rt_err_t rt_usb_detach_instance(uinst_t uinst);
-rt_err_t rt_usb_get_descriptor(uinst_t    uinst,
-                               rt_uint8_t type,
-                               void      *buffer, 
-                               int        nbytes);
-rt_err_t rt_usb_set_configure(uinst_t uinst, int config);
-rt_err_t rt_usb_set_address(uinst_t uinst);
-rt_err_t rt_usb_set_interface(uinst_t uinst, int intf);
-rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature);
-rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t   cfg_desc,
-                                         int           num,
-                                         uintf_desc_t *intf_desc);
-rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc,
-                                        int          num, 
-                                        uep_desc_t  *ep_desc);
+struct uinstance* rt_usbh_alloc_instance(void);
+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_get_descriptor(struct uinstance* device, rt_uint8_t type, void* buffer, 
+    int nbytes);
+rt_err_t rt_usbh_set_configure(struct uinstance* device, int config);
+rt_err_t rt_usbh_set_address(struct uinstance* device);
+rt_err_t rt_usbh_set_interface(struct uinstance* device, int intf);
+rt_err_t rt_usbh_clear_feature(struct uinstance* device, int endpoint, int feature);
+rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num, 
+    uintf_desc_t* intf_desc);
+rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, 
+    uep_desc_t* ep_desc);
 
 /* usb class driver interface */
-rt_err_t rt_usb_class_driver_init(void);
-rt_err_t rt_usb_class_driver_register(ucd_t drv);
-rt_err_t rt_usb_class_driver_unregister(ucd_t drv);
-rt_err_t rt_usb_class_driver_run(ucd_t drv, void *args);
-rt_err_t rt_usb_class_driver_stop(ucd_t drv, void *args);
-ucd_t rt_usb_class_driver_find(int class_code, int subclass_code);
+rt_err_t rt_usbh_class_driver_init(void);
+rt_err_t rt_usbh_class_driver_register(ucd_t drv);
+rt_err_t rt_usbh_class_driver_unregister(ucd_t drv);
+rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args);
+rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args);
+ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code);
 
 /* usb class driver implement */
-ucd_t rt_usb_class_driver_hid(void);
-ucd_t rt_usb_class_driver_hub(void);
-ucd_t rt_usb_class_driver_storage(void);
-ucd_t rt_usb_class_driver_adk(void);
+ucd_t rt_usbh_class_driver_hid(void);
+ucd_t rt_usbh_class_driver_hub(void);
+ucd_t rt_usbh_class_driver_storage(void);
+ucd_t rt_usbh_class_driver_adk(void);
 
 /* usb hid protocal implement */
-uprotocal_t rt_usb_hid_protocal_kbd(void);
-uprotocal_t rt_usb_hid_protocal_mouse(void);
+uprotocal_t rt_usbh_hid_protocal_kbd(void);
+uprotocal_t rt_usbh_hid_protocal_mouse(void);
 
 /* usb adk class driver interface */
-rt_err_t rt_usb_adk_set_string(const char *manufacturer,
-                               const char *model,
-                               const char *description,
-                               const char *version,
-                               const char *uri, 
-                               const char *serial);
+rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
+    const char* description, const char* version, const char* uri, 
+    const char* serial);
 
 /* usb hub interface */
-rt_err_t rt_usb_hub_get_descriptor(uinst_t     uinst,
-                                   rt_uint8_t *buffer, 
-                                   rt_size_t   size);
-rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t *buffer);
-rt_err_t rt_usb_hub_get_port_status(uhubinst_t  uhub,
-                                    rt_uint16_t port, 
-                                    rt_uint8_t *buffer);
-rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t  uhub,
-                                       rt_uint16_t port, 
-                                       rt_uint16_t feature);
-rt_err_t rt_usb_hub_set_port_feature(uhubinst_t  uhub,
-                                     rt_uint16_t port, 
-                                     rt_uint16_t feature);
-rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port);
-rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size);
+rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, 
+    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_port_status(uhub_t uhub, rt_uint16_t port, 
+    rt_uint8_t* buffer);
+rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port, 
+    rt_uint16_t feature);
+rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port, 
+    rt_uint16_t feature);
+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);
 
 /* usb host controller driver interface */
-rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t        hcd,
-                                         upipe_t      *pipe, 
-                                         uifinst_t     ifinst,
-                                         uep_desc_t    ep,
-                                         func_callback callback)
+rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe, 
+    struct uintf* intf, uep_desc_t ep, func_callback callback)
 {
-    if (ifinst == RT_NULL)
-        return -RT_EIO;
+    if(intf == RT_NULL) return -RT_EIO;
 
-    return hcd->ops->alloc_pipe(pipe, ifinst, ep, callback);
+    return hcd->ops->alloc_pipe(pipe, intf, ep, callback);
 }
 
 rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
@@ -255,59 +240,40 @@ rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
     return hcd->ops->free_pipe(pipe);
 }
 
-rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t  hcd,
-                                   upipe_t pipe,
-                                   void   *buffer, 
-                                   int     nbytes,
-                                   int     timeout)
+rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, 
+    int nbytes, int timeout)
 {
-    if (pipe == RT_NULL)
-        return -1;
-    if (pipe->ifinst == RT_NULL)
-        return -1;
-    if (pipe->ifinst->uinst == RT_NULL)
-        return -1;    
-    if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE) 
+    if(pipe == RT_NULL) return -1;
+    if(pipe->intf == RT_NULL) return -1;
+    if(pipe->intf->device == RT_NULL) return -1;    
+    if(pipe->intf->device->status == DEV_STATUS_IDLE) 
         return -1;
 
     return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
 }
 
-rt_inline int rt_usb_hcd_control_xfer(uhcd_t  hcd,
-                                      uinst_t uinst,
-                                      ureq_t  setup, 
-                                      void   *buffer,
-                                      int     nbytes,
-                                      int     timeout)
+rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd, struct uinstance* device, ureq_t setup, 
+    void* buffer, int nbytes, int timeout)
 {
-    if (uinst->status == UINST_STATUS_IDLE)
-        return -1;
+    if(device->status == DEV_STATUS_IDLE) return -1;
 
-    return hcd->ops->ctl_xfer(uinst, setup, buffer, nbytes, timeout);
+    return hcd->ops->ctl_xfer(device, setup, buffer, nbytes, timeout);
 }
 
-rt_inline int rt_usb_hcd_int_xfer(uhcd_t  hcd,
-                                  upipe_t pipe,
-                                  void   *buffer, 
-                                  int     nbytes,
-                                  int     timeout)
+rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, 
+    int nbytes, int timeout)
 {    
-    if (pipe == RT_NULL)
-        return -1;
-    if (pipe->ifinst == RT_NULL)
-        return -1;
-    if (pipe->ifinst->uinst == RT_NULL)
-        return -1;    
-    if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE) 
+    if(pipe == RT_NULL) return -1;
+    if(pipe->intf == RT_NULL) return -1;
+    if(pipe->intf->device == RT_NULL) return -1;    
+    if(pipe->intf->device->status == DEV_STATUS_IDLE) 
         return -1;
 
     return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
 }
 
-rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t      hcd,
-                                          rt_uint16_t port, 
-                                          rt_uint8_t  cmd,
-                                          void       *args)
+rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd, rt_uint16_t port, 
+    rt_uint8_t cmd, void *args)
 {    
     return hcd->ops->hub_ctrl(port, cmd, args);
 }

+ 9 - 9
components/drivers/usb/usbhost/SConscript

@@ -9,22 +9,22 @@ core/usbhost.c
 core/hub.c
 """)
 
-if GetDepend('RT_USB_CLASS_ADK'):
+if GetDepend('RT_USBH_ADK'):
 	src += Glob('class/adk.c')
-	src += Glob('udev/adkapp.c')
+	src += Glob('class/adkapp.c')
 
-if GetDepend('RT_USB_CLASS_MASS_STORAGE'):
+if GetDepend('RT_USBH_MSTORAGE'):
 	src += Glob('class/mass.c')
-	src += Glob('udev/udisk.c')
+	src += Glob('class/udisk.c')
 
-if GetDepend('RT_USB_CLASS_HID'):
+if GetDepend('RT_USBH_HID'):
 	src += Glob('class/hid.c')
 
-if GetDepend('RT_USB_HID_MOUSE'):
-	src += Glob('udev/umouse.c')
+if GetDepend('RT_USBH_HID_MOUSE'):
+	src += Glob('class/umouse.c')
 
-if GetDepend('RT_USB_HID_KEYBOARD'):
-	src += Glob('udev/ukbd.c')
+if GetDepend('RT_USBH_HID_KEYBOARD'):
+	src += Glob('class/ukbd.c')
 
 CPPPATH = [cwd, cwd + '/class', cwd + '/core', \
 	cwd + '/include', cwd + '../../../include']

+ 98 - 95
components/drivers/usb/usbhost/class/adk.c

@@ -26,7 +26,7 @@
 #include <drivers/usb_host.h>
 #include "adk.h"
 
-#ifdef RT_USB_CLASS_ADK
+#ifdef RT_USBH_ADK
 
 static struct uclass_driver adk_driver;
 static const char* _adk_manufacturer = RT_NULL;
@@ -36,7 +36,7 @@ static const char* _adk_version = RT_NULL;
 static const char* _adk_uri = RT_NULL;
 static const char* _adk_serial = RT_NULL;
 
-rt_err_t rt_usb_adk_set_string(const char* manufacturer, const char* model,
+rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
     const char* description, const char* _version, const char* uri, 
     const char* serial)
 {
@@ -53,29 +53,29 @@ rt_err_t rt_usb_adk_set_string(const char* manufacturer, const char* model,
 #ifdef RT_USING_MODULE
 #include <rtm.h>
 
-RTM_EXPORT(rt_usb_adk_set_string);
+RTM_EXPORT(rt_usbh_adk_set_string);
 #endif
 
 /**
  * This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @duration the idle period of requesting data.
  * @report_id the report id
  * 
  * @return the error code, RT_EOK on successfully.
 */
-static rt_err_t rt_usb_adk_get_protocol(uifinst_t ifinst, rt_uint16_t *protocol)
+static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
 {
     struct ureqest setup;
-    uinst_t uinst;    
+    uinst_t device;    
     int timeout = 100;
             
         /* parameter check */
-    RT_ASSERT(ifinst != RT_NULL);
-    RT_ASSERT(ifinst->uinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
+    RT_ASSERT(intf->device != RT_NULL);
 
-    uinst = ifinst->uinst;
+    device = intf->device;
 
     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR | 
         USB_REQ_TYPE_DEVICE;
@@ -84,7 +84,7 @@ static rt_err_t rt_usb_adk_get_protocol(uifinst_t ifinst, rt_uint16_t *protocol)
     setup.length = 2;
     setup.value = 0;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, (void*)protocol, 2, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2, 
         timeout) == 0) return RT_EOK;
     else return -RT_FALSE;    
 }
@@ -92,24 +92,24 @@ static rt_err_t rt_usb_adk_get_protocol(uifinst_t ifinst, rt_uint16_t *protocol)
 /**
  * This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @duration the idle period of requesting data.
  * @report_id the report id
  * 
  * @return the error code, RT_EOK on successfully.
 */
-static rt_err_t rt_usb_adk_send_string(uifinst_t ifinst, rt_uint16_t index, 
+static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, 
     const char* str)
 {
     struct ureqest setup;
-    uinst_t uinst;    
+    uinst_t device;    
     int timeout = 100;
             
         /* parameter check */
-    RT_ASSERT(ifinst != RT_NULL);
-    RT_ASSERT(ifinst->uinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
+    RT_ASSERT(intf->device != RT_NULL);
 
-    uinst = ifinst->uinst;
+    device = intf->device;
 
     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | 
         USB_REQ_TYPE_DEVICE;
@@ -118,7 +118,7 @@ static rt_err_t rt_usb_adk_send_string(uifinst_t ifinst, rt_uint16_t index,
     setup.length = rt_strlen(str) + 1;
     setup.value = 0;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, (void*)str, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str, 
         rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
     else return -RT_FALSE;   
 }
@@ -126,23 +126,23 @@ static rt_err_t rt_usb_adk_send_string(uifinst_t ifinst, rt_uint16_t index,
 /**
  * This function will do USB_REQ_START request to set idle period to the usb adk device
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @duration the idle period of requesting data.
  * @report_id the report id
  * 
  * @return the error code, RT_EOK on successfully.
 */
-static rt_err_t rt_usb_adk_start(uifinst_t ifinst)
+static rt_err_t rt_usbh_adk_start(struct uintf* intf)
 {
     struct ureqest setup;
-    uinst_t uinst;    
+    uinst_t device;    
     int timeout = 100;
             
         /* parameter check */
-    RT_ASSERT(ifinst != RT_NULL);
-    RT_ASSERT(ifinst->uinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
+    RT_ASSERT(intf->device != RT_NULL);
 
-    uinst = ifinst->uinst;
+    device = intf->device;
 
     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | 
         USB_REQ_TYPE_DEVICE;
@@ -151,7 +151,7 @@ static rt_err_t rt_usb_adk_start(uifinst_t ifinst)
     setup.length = 0;
     setup.value = 0;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
         timeout) == 0) return RT_EOK;
     else return -RT_FALSE;   
 }
@@ -159,25 +159,25 @@ static rt_err_t rt_usb_adk_start(uifinst_t ifinst)
 /**
  * This function will read data from usb adk device
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * 
  * @return the error code, RT_EOK on successfully.
 */
-static rt_size_t rt_usb_adk_read(rt_device_t device, rt_off_t pos, void* buffer, 
+static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer, 
     rt_size_t size)
 {
-    uadkinst_t adkinst;
+    uadk_t adk;
     rt_size_t length;
-    uifinst_t ifinst;
+    struct uintf* intf;
 
     /* check parameter */
     RT_ASSERT(device != RT_NULL);
     RT_ASSERT(buffer != RT_NULL);
 
-    ifinst = (uifinst_t)device->user_data;
-    adkinst = (uadkinst_t)ifinst->user_data;
+    intf = (struct uintf*)device->user_data;
+    adk = (uadk_t)intf->user_data;
 
-    length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_in, 
+    length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in, 
         buffer, size, 300);
     
     return length;
@@ -187,23 +187,23 @@ static rt_size_t rt_usb_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
 /**
  * This function will write data to usb adk device
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * 
  * @return the error code, RT_EOK on successfully.
 */
-static rt_size_t rt_usb_adk_write (rt_device_t device, rt_off_t pos, const void* buffer, 
+static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer, 
     rt_size_t size)
 {
-    uadkinst_t adkinst;
+    uadk_t adk;
     rt_size_t length;
-    uifinst_t ifinst;
+    struct uintf* intf;
 
     RT_ASSERT(buffer != RT_NULL);    
 
-    ifinst = (uifinst_t)device->user_data;
-    adkinst = (uadkinst_t)ifinst->user_data;
+    intf = (struct uintf*)device->user_data;
+    adk = (uadk_t)intf->user_data;
 
-    length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_out, 
+    length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out, 
         (void*)buffer, size, 300);
     
     return length;
@@ -217,30 +217,30 @@ static rt_size_t rt_usb_adk_write (rt_device_t device, rt_off_t pos, const void*
  * 
  * @return the error code, RT_EOK on successfully.
  */
-static rt_err_t rt_usb_adk_run(void* arg)
+static rt_err_t rt_usbh_adk_enable(void* arg)
 {
     int i = 0;
-    uadkinst_t adkinst;
-    uifinst_t ifinst = (uifinst_t)arg;
+    uadk_t adk;
+    struct uintf* intf = (struct uintf*)arg;
     udev_desc_t dev_desc;
     rt_uint16_t protocol;
     rt_err_t ret;    
     
     /* parameter check */
-    if(ifinst == RT_NULL)
+    if(intf == RT_NULL)
     {
         rt_kprintf("the interface is not available\n");
         return -RT_EIO;
     }
 
-    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_adk_run\n"));
+    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n"));
         
-    dev_desc = &ifinst->uinst->dev_desc;
+    dev_desc = &intf->device->dev_desc;
     if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID && 
         (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID || 
         dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
     {
-        if(ifinst->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
+        if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
     
         RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n"));        
     }
@@ -248,9 +248,9 @@ static rt_err_t rt_usb_adk_run(void* arg)
     {
         RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n"));        
         
-        if((ret = rt_usb_adk_get_protocol(ifinst, &protocol)) != RT_EOK)
+        if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK)
         {
-            rt_kprintf("rt_usb_adk_get_protocol failed\n");
+            rt_kprintf("rt_usbh_adk_get_protocol failed\n");
             return ret;
         }
 
@@ -260,17 +260,17 @@ static rt_err_t rt_usb_adk_run(void* arg)
             return -RT_ERROR;
         }       
 
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_MODEL, _adk_model);
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_DESCRIPTION, _adk_description);
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_VERSION, _adk_version);
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_URI, _adk_uri);        
-        rt_usb_adk_send_string(ifinst, 
+        rt_usbh_adk_send_string(intf, 
             ACCESSORY_STRING_SERIAL, _adk_serial);            
 
         RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer));
@@ -280,28 +280,28 @@ static rt_err_t rt_usb_adk_run(void* arg)
         RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri));       
         RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _adk_serial));               
         
-        if((ret = rt_usb_adk_start(ifinst)) != RT_EOK)
+        if((ret = rt_usbh_adk_start(intf)) != RT_EOK)
         {
-            rt_kprintf("rt_usb_adk_start failed\n");
+            rt_kprintf("rt_usbh_adk_start failed\n");
             return ret;
         }        
 
         return RT_EOK;
     }
     
-    adkinst = rt_malloc(sizeof(struct uadkinst));
-    RT_ASSERT(adkinst != RT_NULL);
+    adk = rt_malloc(sizeof(struct uadkinst));
+    RT_ASSERT(adk != RT_NULL);
 
     /* initilize the data structure */
-    rt_memset(adkinst, 0, sizeof(struct uadkinst));    
-    ifinst->user_data = (void*)adkinst;
+    rt_memset(adk, 0, sizeof(struct uadkinst));    
+    intf->user_data = (void*)adk;
 
-    for(i=0; i<ifinst->intf_desc->bNumEndpoints; i++)
+    for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
     {        
         uep_desc_t ep_desc;
         
         /* get endpoint descriptor from interface descriptor */
-        rt_usb_get_endpoint_descriptor(ifinst->intf_desc, i, &ep_desc);
+        rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
         if(ep_desc == RT_NULL)
         {
             rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
@@ -316,41 +316,41 @@ static rt_err_t rt_usb_adk_run(void* arg)
         if(ep_desc->bEndpointAddress & USB_DIR_IN)
         {
             /* allocate an in pipe for the adk instance */
-            ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_in, 
-                ifinst, ep_desc, RT_NULL);
+            ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in, 
+                intf, ep_desc, RT_NULL);
             if(ret != RT_EOK) return ret;
         }
         else
         {        
             /* allocate an output pipe for the adk instance */
-            ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_out, 
-                ifinst, ep_desc, RT_NULL);            
+            ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out, 
+                intf, ep_desc, RT_NULL);            
             if(ret != RT_EOK) return ret;
         }
     }
 
     /* check pipes infomation */
-    if(adkinst->pipe_in == RT_NULL || adkinst->pipe_out == RT_NULL)
+    if(adk->pipe_in == RT_NULL || adk->pipe_out == RT_NULL)
     {
         rt_kprintf("pipe error, unsupported device\n");
         return -RT_ERROR;
     }    
 
     /* set configuration */
-    ret = rt_usb_set_configure(ifinst->uinst, 1);
+    ret = rt_usbh_set_configure(intf->device, 1);
     if(ret != RT_EOK) return ret;
 
     /* register adk device */
-    adkinst->device.type  = RT_Device_Class_Char;                         
-    adkinst->device.init = RT_NULL;         
-    adkinst->device.open = RT_NULL;         
-    adkinst->device.close = RT_NULL;                 
-    adkinst->device.read = rt_usb_adk_read;
-    adkinst->device.write = rt_usb_adk_write;
-    adkinst->device.control = RT_NULL;
-    adkinst->device.user_data = (void*)ifinst;
-
-    rt_device_register(&adkinst->device, "adkdev", RT_DEVICE_FLAG_RDWR);
+    adk->device.type  = RT_Device_Class_Char;                         
+    adk->device.init = RT_NULL;         
+    adk->device.open = RT_NULL;         
+    adk->device.close = RT_NULL;                 
+    adk->device.read = rt_usbh_adk_read;
+    adk->device.write = rt_usbh_adk_write;
+    adk->device.control = RT_NULL;
+    adk->device.user_data = (void*)intf;
+
+    rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
     
     return RT_EOK;
 }
@@ -363,36 +363,39 @@ static rt_err_t rt_usb_adk_run(void* arg)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-static rt_err_t rt_usb_adk_stop(void* arg)
+static rt_err_t rt_usbh_adk_disable(void* arg)
 {
-    uadkinst_t adkinst;
-    uifinst_t ifinst = (uifinst_t)arg;
+    uadk_t adk;
+    struct uintf* intf = (struct uintf*)arg;
 
-    RT_ASSERT(ifinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
 
-    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_adk_stop\n"));
+    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n"));
 
-    adkinst = (uadkinst_t)ifinst->user_data;
-    if(adkinst == RT_NULL) 
+    adk = (uadk_t)intf->user_data;
+    if(adk == RT_NULL) 
     {
-        rt_free(ifinst);    
+        rt_free(intf);    
         return RT_EOK;
     }
     
-    if(adkinst->pipe_in != RT_NULL)
-        rt_usb_hcd_free_pipe(ifinst->uinst->hcd, adkinst->pipe_in);
+    if(adk->pipe_in != RT_NULL)
+        rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
 
-    if(adkinst->pipe_out != RT_NULL)
-        rt_usb_hcd_free_pipe(ifinst->uinst->hcd, adkinst->pipe_out);
+    if(adk->pipe_out != RT_NULL)
+        rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_out);
 
     /* unregister adk device */
-    rt_device_unregister(&adkinst->device);
+    rt_device_unregister(&adk->device);
 
     /* free adk instance */
-    if(adkinst != RT_NULL) rt_free(adkinst);
+    if(adk != RT_NULL) 
+    {
+        rt_free(adk);
+    }
     
     /* free interface instance */
-    rt_free(ifinst);
+    rt_free(intf);
 
     return RT_EOK;
 }
@@ -403,12 +406,12 @@ static rt_err_t rt_usb_adk_stop(void* arg)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-ucd_t rt_usb_class_driver_adk(void)
+ucd_t rt_usbh_class_driver_adk(void)
 {
     adk_driver.class_code = USB_CLASS_ADK;
     
-    adk_driver.run = rt_usb_adk_run;
-    adk_driver.stop = rt_usb_adk_stop;
+    adk_driver.enable = rt_usbh_adk_enable;
+    adk_driver.disable = rt_usbh_adk_disable;
 
     return &adk_driver;
 }

+ 1 - 1
components/drivers/usb/usbhost/class/adk.h

@@ -34,7 +34,7 @@ struct uadkinst
 
     struct rt_device device;
 };    
-typedef struct uadkinst* uadkinst_t;
+typedef struct uadkinst* uadk_t;
 
 #define USB_ACCESSORY_VENDOR_ID         0x18D1
 #define USB_ACCESSORY_PRODUCT_ID        0x2D00

+ 69 - 70
components/drivers/usb/usbhost/core/core.c

@@ -25,7 +25,7 @@
 #include <rtthread.h>
 #include <drivers/usb_host.h>
 
-static struct uinstance uinst[USB_MAX_DEVICE];
+static struct uinstance dev[USB_MAX_DEVICE];
 
 /**
  * This function will allocate an usb device instance from system.
@@ -35,7 +35,7 @@ static struct uinstance uinst[USB_MAX_DEVICE];
  *
  * @return the allocate instance on successful, or RT_NULL on failure.
  */
-uinst_t rt_usb_alloc_instance(void)
+uinst_t rt_usbh_alloc_instance(void)
 {
     int i;
 
@@ -45,19 +45,19 @@ uinst_t rt_usb_alloc_instance(void)
     for(i=0; i<USB_MAX_DEVICE; i++)
     {
         /* to find an idle instance handle */
-        if(uinst[i].status != UINST_STATUS_IDLE) continue;
+        if(dev[i].status != DEV_STATUS_IDLE) continue;
         
         /* initialize the usb device instance */
-        rt_memset(&uinst[i], 0, sizeof(struct uinstance));
+        rt_memset(&dev[i], 0, sizeof(struct uinstance));
         
-        uinst[i].status = UINST_STATUS_BUSY;
-        uinst[i].index = i + 1;
-        uinst[i].address = 0;
-        uinst[i].max_packet_size = 0x8;
+        dev[i].status = DEV_STATUS_BUSY;
+        dev[i].index = i + 1;
+        dev[i].address = 0;
+        dev[i].max_packet_size = 0x8;
 
         /* unlock scheduler */        
         rt_exit_critical();
-        return &uinst[i];
+        return &dev[i];
     }
 
     /* unlock scheduler */            
@@ -71,11 +71,11 @@ uinst_t rt_usb_alloc_instance(void)
  * and do device enumunation process.
  *
  * @param hcd the host controller driver.
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_attatch_instance(uinst_t uinst)
+rt_err_t rt_usbh_attatch_instance(uinst_t device)
 {
     int i = 0;
     rt_err_t ret = RT_EOK;
@@ -84,15 +84,15 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     uintf_desc_t intf_desc;
     ucd_t drv;
 
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
     
     rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor));
-    dev_desc = &uinst->dev_desc;
+    dev_desc = &device->dev_desc;
 
     RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n"));
     
     /* get device descriptor head */
-    ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
+    ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
     if(ret != RT_EOK)
     {
         rt_kprintf("get device descriptor head failed\n");
@@ -100,7 +100,7 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     }
     
     /* set device address */
-    ret = rt_usb_set_address(uinst);
+    ret = rt_usbh_set_address(device);
     if(ret != RT_EOK)
     {
         rt_kprintf("set device address failed\n");
@@ -108,14 +108,14 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     }
 
     /* set device max packet size */
-    uinst->max_packet_size = uinst->dev_desc.bMaxPacketSize0;
+    device->max_packet_size = device->dev_desc.bMaxPacketSize0;
 
     RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n",
                                 dev_desc->bLength));
     
     /* get full device descriptor again */
-    ret = rt_usb_get_descriptor
-        (uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
+    ret = rt_usbh_get_descriptor
+        (device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
     if(ret != RT_EOK)
     {
         rt_kprintf("get full device descriptor failed\n");
@@ -126,8 +126,7 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     RT_DEBUG_LOG(RT_DEBUG_USB, ("Product ID 0x%x\n", dev_desc->idProduct));
 
     /* get configuration descriptor head */
-    ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 
-        sizeof(struct uconfig_descriptor));
+    ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18);
     if(ret != RT_EOK)
     {
         rt_kprintf("get configuration descriptor head failed\n");
@@ -135,12 +134,12 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     }
 
     /* alloc memory for configuration descriptor */
-    uinst->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
-    rt_memset(uinst->cfg_desc, 0, cfg_desc.wTotalLength);
+    device->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
+    rt_memset(device->cfg_desc, 0, cfg_desc.wTotalLength);
 
     /* get full configuration descriptor */
-    ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION, 
-        uinst->cfg_desc, cfg_desc.wTotalLength);
+    ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, 
+        device->cfg_desc, cfg_desc.wTotalLength);
     if(ret != RT_EOK)
     {
         rt_kprintf("get full configuration descriptor failed\n");
@@ -148,13 +147,13 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
     }
 
     /* set configuration */
-    ret = rt_usb_set_configure(uinst, 1);
+    ret = rt_usbh_set_configure(device, 1);
     if(ret != RT_EOK) return ret;
 
-    for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
+    for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
     {        
         /* get interface descriptor through configuration descriptor */
-        ret = rt_usb_get_interface_descriptor(uinst->cfg_desc, i, &intf_desc);
+        ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc);
         if(ret != RT_EOK)
         {
             rt_kprintf("rt_usb_get_interface_descriptor error\n");
@@ -166,22 +165,22 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
                                     intf_desc->bInterfaceSubClass));
 
         /* find driver by class code found in interface descriptor */
-        drv = rt_usb_class_driver_find(intf_desc->bInterfaceClass, 
+        drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass, 
             intf_desc->bInterfaceSubClass);
         
         if(drv != RT_NULL)
         {
-            /* allocate memory for interface uinst */
-            uinst->ifinst[i] = 
-                (uifinst_t)rt_malloc(sizeof(struct uifinst));
+            /* allocate memory for interface device */
+            device->intf[i] = 
+                (struct uintf*)rt_malloc(sizeof(struct uintf));
 
-            uinst->ifinst[i]->drv = drv;
-            uinst->ifinst[i]->uinst = uinst;
-            uinst->ifinst[i]->intf_desc = intf_desc;
-            uinst->ifinst[i]->user_data = RT_NULL;
+            device->intf[i]->drv = drv;
+            device->intf[i]->device = device;
+            device->intf[i]->intf_desc = intf_desc;
+            device->intf[i]->user_data = RT_NULL;
 
             /* open usb class driver */
-            ret = rt_usb_class_driver_run(drv, (void*)uinst->ifinst[i]);
+            ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]);
             if(ret != RT_EOK)
             {
                 rt_kprintf("interface %d run class driver error\n", i);
@@ -201,35 +200,35 @@ rt_err_t rt_usb_attatch_instance(uinst_t uinst)
  * This function will detach an usb device instance from its host controller,
  * and release all resource.
  *
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_detach_instance(uinst_t uinst)
+rt_err_t rt_usbh_detach_instance(uinst_t device)
 {
     int i = 0;
 
-    if(uinst == RT_NULL) 
+    if(device == RT_NULL) 
     {
         rt_kprintf("no usb instance to detach\n");
         return -RT_ERROR;
     }
     
     /* free configration descriptor */
-    if(uinst->cfg_desc) rt_free(uinst->cfg_desc);
+    if(device->cfg_desc) rt_free(device->cfg_desc);
     
-    for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
+    for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
     {
-        if(uinst->ifinst[i] == RT_NULL) continue;
-        if(uinst->ifinst[i]->drv == RT_NULL) continue;
+        if(device->intf[i] == RT_NULL) continue;
+        if(device->intf[i]->drv == RT_NULL) continue;
 
-        RT_ASSERT(uinst->ifinst[i]->uinst == uinst);
+        RT_ASSERT(device->intf[i]->device == device);
 
         RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
-        rt_usb_class_driver_stop(uinst->ifinst[i]->drv, (void*)uinst->ifinst[i]);
+        rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
     }
     
-    rt_memset(uinst, 0, sizeof(struct uinstance));
+    rt_memset(device, 0, sizeof(struct uinstance));
     
     return RT_EOK;
 }
@@ -237,20 +236,20 @@ rt_err_t rt_usb_detach_instance(uinst_t uinst)
 /**
  * This function will do USB_REQ_GET_DESCRIPTO' request for the usb device instance,
  *
- * @param uinst the usb device instance. 
+ * @param device the usb device instance. 
  * @param type the type of descriptor request.
  * @param buffer the data buffer to save requested data
  * @param nbytes the size of buffer
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer, 
+rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer, 
     int nbytes)
 {
     struct ureqest setup;
     int timeout = 100;
     
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD | 
         USB_REQ_TYPE_DEVICE;
@@ -259,7 +258,7 @@ rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer,
     setup.length = nbytes;
     setup.value = type << 8;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, buffer, nbytes, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, nbytes, 
         timeout) != nbytes) return -RT_EIO;
     else return RT_EOK;
 }
@@ -267,16 +266,16 @@ rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer,
 /**
  * This function will set an address to the usb device.
  *
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_set_address(uinst_t uinst)
+rt_err_t rt_usbh_set_address(uinst_t device)
 {
     struct ureqest setup;
     int timeout = 100;
     
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_set_address\n"));
 
@@ -285,14 +284,14 @@ rt_err_t rt_usb_set_address(uinst_t uinst)
     setup.request = USB_REQ_SET_ADDRESS;
     setup.index = 0;
     setup.length = 0;
-    setup.value = uinst->index;
+    setup.value = device->index;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
         timeout) != 0) return -RT_EIO;
 
     rt_thread_delay(50);
 
-    uinst->address = uinst->index;
+    device->address = device->index;
     
     return RT_EOK;
 }
@@ -300,18 +299,18 @@ rt_err_t rt_usb_set_address(uinst_t uinst)
 /**
  * This function will set a configuration to the usb device.
  *
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * @param config the configuration number.
   * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_set_configure(uinst_t uinst, int config)
+rt_err_t rt_usbh_set_configure(uinst_t device, int config)
 {
     struct ureqest setup;
     int timeout = 100;
 
     /* check parameter */
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD | 
         USB_REQ_TYPE_DEVICE;
@@ -320,7 +319,7 @@ rt_err_t rt_usb_set_configure(uinst_t uinst, int config)
     setup.length = 0;
     setup.value = config;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
         timeout) != 0) return -RT_EIO;
     
     return RT_EOK;    
@@ -329,18 +328,18 @@ rt_err_t rt_usb_set_configure(uinst_t uinst, int config)
 /**
  * This function will set an interface to the usb device.
  *
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * @param intf the interface number.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_set_interface(uinst_t uinst, int intf)
+rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
 {
     struct ureqest setup;
     int timeout = 100;
 
     /* check parameter */
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD | 
         USB_REQ_TYPE_INTERFACE;
@@ -349,7 +348,7 @@ rt_err_t rt_usb_set_interface(uinst_t uinst, int intf)
     setup.length = 0;
     setup.value = intf;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
         timeout) != 0) return -RT_EIO;
     
     return RT_EOK;    
@@ -358,18 +357,18 @@ rt_err_t rt_usb_set_interface(uinst_t uinst, int intf)
 /**
  * This function will clear feature for the endpoint of the usb device.
  *
- * @param uinst the usb device instance.
+ * @param device the usb device instance.
  * @param endpoint the endpoint number of the usb device.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature)
+rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
 {
     struct ureqest setup;
     int timeout = 100;
 
     /* check parameter */
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD | 
         USB_REQ_TYPE_ENDPOINT;
@@ -378,7 +377,7 @@ rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature)
     setup.length = 0;
     setup.value = feature;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, 
         timeout) != 0) return -RT_EIO;
     
     return RT_EOK;    
@@ -393,7 +392,7 @@ rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc, int num, 
+rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num, 
     uintf_desc_t* intf_desc)
 {
     rt_uint32_t ptr, depth = 0;
@@ -438,7 +437,7 @@ rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, 
+rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num, 
     uep_desc_t* ep_desc)
 {
     int count = 0, depth = 0;

+ 10 - 10
components/drivers/usb/usbhost/core/driver.c

@@ -34,7 +34,7 @@ static rt_list_t _driver_list;
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_class_driver_init(void)
+rt_err_t rt_usbh_class_driver_init(void)
 {
     rt_list_init(&_driver_list);
 
@@ -49,7 +49,7 @@ rt_err_t rt_usb_class_driver_init(void)
  * @return the error code, RT_EOK on successfully.
  */
 
-rt_err_t rt_usb_class_driver_register(ucd_t drv)
+rt_err_t rt_usbh_class_driver_register(ucd_t drv)
 {
     if (drv == RT_NULL) return -RT_ERROR;
 
@@ -66,7 +66,7 @@ rt_err_t rt_usb_class_driver_register(ucd_t drv)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_class_driver_unregister(ucd_t drv)
+rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
 {
     RT_ASSERT(drv != RT_NULL);
 
@@ -84,12 +84,12 @@ rt_err_t rt_usb_class_driver_unregister(ucd_t drv)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_class_driver_run(ucd_t drv, void* args)
+rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
 {
     RT_ASSERT(drv != RT_NULL);
 
-    if(drv->run != RT_NULL)
-        drv->run(args);
+    if(drv->enable != RT_NULL)
+        drv->enable(args);
 
     return RT_EOK;
 }
@@ -102,12 +102,12 @@ rt_err_t rt_usb_class_driver_run(ucd_t drv, void* args)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_class_driver_stop(ucd_t drv, void* args)
+rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
 {
     RT_ASSERT(drv != RT_NULL);
 
-    if(drv->stop != RT_NULL)
-        drv->stop(args);
+    if(drv->disable != RT_NULL)
+        drv->disable(args);
 
     return RT_EOK;
 }
@@ -121,7 +121,7 @@ rt_err_t rt_usb_class_driver_stop(ucd_t drv, void* args)
  *
  * @return the registered usb class driver on successful, or RT_NULL on failure.
  */
-ucd_t rt_usb_class_driver_find(int class_code, int subclass_code)
+ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code)
 {
     struct rt_list_node *node;
 

+ 114 - 115
components/drivers/usb/usbhost/core/hub.c

@@ -34,20 +34,20 @@ static struct uclass_driver hub_driver;
  * This function will do USB_REQ_GET_DESCRIPTOR request for the device instance 
  * to get usb hub descriptor.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @buffer the data buffer to save usb hub descriptor.
  * @param nbytes the size of buffer
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst, rt_uint8_t *buffer, 
+rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, 
     rt_size_t nbytes)
 {
     struct ureqest setup;
     int timeout = 100;
         
     /* parameter check */
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
     
     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | 
         USB_REQ_TYPE_DEVICE;
@@ -56,7 +56,7 @@ rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst, rt_uint8_t *buffer,
     setup.length = nbytes;
     setup.value = USB_DESC_TYPE_HUB << 8;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, buffer, nbytes, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, nbytes, 
         timeout) == nbytes) return RT_EOK;
     else return -RT_FALSE;    
 }
@@ -65,19 +65,19 @@ rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst, rt_uint8_t *buffer,
  * This function will do USB_REQ_GET_STATUS request for the device instance 
  * to get usb hub status.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @buffer the data buffer to save usb hub status.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t* buffer)
+rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer)
 {
     struct ureqest setup;
     int timeout = 100;
     int length = 4;
     
     /* parameter check */
-    RT_ASSERT(uinst != RT_NULL);
+    RT_ASSERT(device != RT_NULL);
 
     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | 
         USB_REQ_TYPE_DEVICE;
@@ -86,7 +86,7 @@ rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t* buffer)
     setup.length = length;
     setup.value = 0;
 
-    if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, buffer, length, 
+    if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, length, 
         timeout) == length) return RT_EOK;
     else return -RT_FALSE;    
 }
@@ -95,13 +95,13 @@ rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t* buffer)
  * This function will do USB_REQ_GET_STATUS request for the device instance 
  * to get hub port status.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @port the hub port to get status.
  * @buffer the data buffer to save usb hub status.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub, rt_uint16_t port, 
+rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, 
     rt_uint8_t* buffer)
 {
     struct ureqest setup;
@@ -109,12 +109,12 @@ rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub, rt_uint16_t port,
     int length = 4;
     
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
 
     /* get roothub port status */
-    if(uhub->is_roothub)
+    if(hub->is_roothub)
     {
-        rt_usb_hcd_hub_control(uhub->hcd, port, RH_GET_PORT_STATUS, 
+        rt_usb_hcd_hub_control(hub->hcd, port, RH_GET_PORT_STATUS, 
             (void*)buffer);
         return RT_EOK;
     }
@@ -126,7 +126,7 @@ rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub, rt_uint16_t port,
     setup.length = 4;
     setup.value = 0;
 
-    if(rt_usb_hcd_control_xfer(uhub->hcd, uhub->self, &setup, buffer, 
+    if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, buffer, 
         length, timeout) == timeout) return RT_EOK;
     else return -RT_FALSE;        
 }
@@ -135,25 +135,25 @@ rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub, rt_uint16_t port,
  * This function will do USB_REQ_CLEAR_FEATURE request for the device instance 
  * to clear feature of the hub port.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @port the hub port.
  * @feature feature to be cleared.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub, rt_uint16_t port, 
+rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, 
     rt_uint16_t feature)
 {
     struct ureqest setup;
     int timeout = 100;
         
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
 
     /* clear roothub feature */
-    if(uhub->is_roothub)
+    if(hub->is_roothub)
     {
-        rt_usb_hcd_hub_control(uhub->hcd, port, RH_CLEAR_PORT_FEATURE, 
+        rt_usb_hcd_hub_control(hub->hcd, port, RH_CLEAR_PORT_FEATURE, 
             (void*)feature);
         return RT_EOK;
     }
@@ -165,7 +165,7 @@ rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub, rt_uint16_t port,
     setup.length = 0;
     setup.value = feature;
 
-    if(rt_usb_hcd_control_xfer(uhub->hcd, uhub->self, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0, 
         timeout) == 0) return RT_EOK;
     else return -RT_FALSE;    
 }
@@ -174,25 +174,25 @@ rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub, rt_uint16_t port,
  * This function will do USB_REQ_SET_FEATURE request for the device instance 
  * to set feature of the hub port.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @port the hub port.
  * @feature feature to be set.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub, rt_uint16_t port, 
+rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port, 
     rt_uint16_t feature)
 {
     struct ureqest setup;
     int timeout = 100;
         
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
 
     /* clear roothub feature */
-    if(uhub->is_roothub)
+    if(hub->is_roothub)
     {
-        rt_usb_hcd_hub_control(uhub->hcd, port, RH_SET_PORT_FEATURE, 
+        rt_usb_hcd_hub_control(hub->hcd, port, RH_SET_PORT_FEATURE, 
             (void*)feature);
         return RT_EOK;
     }
@@ -204,7 +204,7 @@ rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub, rt_uint16_t port,
     setup.length = 0;
     setup.value = feature;
 
-    if(rt_usb_hcd_control_xfer(uhub->hcd, uhub->self, &setup, RT_NULL, 0, 
+    if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0, 
         timeout) == 0) return RT_EOK;
     else return -RT_FALSE;        
 }
@@ -212,33 +212,33 @@ rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub, rt_uint16_t port,
 /**
  * This function will rest hub port, it is invoked when sub device attached to the hub port.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * @param port the hub port.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port)
+rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
 {
     rt_err_t ret;
     rt_uint32_t pstatus;
     
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
     
     rt_thread_delay(50);
 
     /* reset hub port */
-    ret = rt_usb_hub_set_port_feature(uhub, port, PORT_FEAT_RESET);
+    ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
     if(ret != RT_EOK) return ret;
 
     while(1)
     {
-        ret = rt_usb_hub_get_port_status(uhub, port, (rt_uint8_t*)&pstatus);
+        ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
         if(!(pstatus & PORT_PRS)) break;
     }
     
     /* clear port reset feature */
-    ret = rt_usb_hub_clear_port_feature(uhub, port, PORT_FEAT_C_RESET);    
+    ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);    
     if(ret != RT_EOK) return ret;
 
     rt_thread_delay(50);    
@@ -249,12 +249,12 @@ rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port)
 /**
  * This function will do debouce, it is invoked when sub device attached to the hub port.
  *
- * @param uinst the usb instance.
+ * @param device the usb instance.
  * @param port the hub port.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-rt_err_t rt_usb_hub_port_debounce(uhubinst_t uhub, rt_uint16_t port)
+rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
 {
     rt_err_t ret;
     int i = 0, times = 20;
@@ -262,11 +262,11 @@ rt_err_t rt_usb_hub_port_debounce(uhubinst_t uhub, rt_uint16_t port)
     rt_bool_t connect = RT_TRUE;
 
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
 
     for(i=0; i<times; i++)
     {
-        ret = rt_usb_hub_get_port_status(uhub, port, (rt_uint8_t*)&pstatus);
+        ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
         if(ret != RT_EOK) return ret;
             
         if(!(pstatus & PORT_CCS)) 
@@ -286,70 +286,70 @@ rt_err_t rt_usb_hub_port_debounce(uhubinst_t uhub, rt_uint16_t port)
  * This function will poll all the hub ports to detect port status, especially connect and
  * disconnect events.
  *
- * @param ifinst the interface instance.
+ * @param intf the interface instance.
  * 
  * @return the error code, RT_EOK on successfully.
  */
-static rt_err_t rt_usb_hub_port_change(uhubinst_t uhub)
+static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
 {
     int i;
     rt_bool_t reconnect;
 
     /* parameter check */
-    RT_ASSERT(uhub != RT_NULL);
+    RT_ASSERT(hub != RT_NULL);
 
     /* get usb device instance */    
-    for (i = 0; i < uhub->num_ports; i++)
+    for (i = 0; i < hub->num_ports; i++)
     {
         rt_err_t ret;
-        uinst_t uinst;
+        struct uinstance* device;
         rt_uint32_t pstatus = 0;
 
         reconnect = RT_FALSE;
         
         /* get hub port status */
-        ret = rt_usb_hub_get_port_status(uhub, i + 1, (rt_uint8_t*)&pstatus);
+        ret = rt_usbh_hub_get_port_status(hub, i + 1, (rt_uint8_t*)&pstatus);
         if(ret != RT_EOK) continue;
 
-        RT_DEBUG_LOG(RT_DEBUG_USB, ("port_status 0x%x\n", pstatus));
+        RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i, pstatus));
 
         /* check port status change */
         if ((pstatus & PORT_CCSC)) 
         {        
             /* clear port status change feature */
-            rt_usb_hub_clear_port_feature(uhub, i + 1, PORT_FEAT_C_CONNECTION);
+            rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
             reconnect = RT_TRUE;
         }
 
         if(pstatus & PORT_PESC)
         {
-            rt_usb_hub_clear_port_feature(uhub, i + 1, PORT_FEAT_C_ENABLE);            
+            rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);            
             reconnect = RT_TRUE;
         }
         
         if(reconnect)
         {            
-            if(uhub->child[i]->status != UINST_STATUS_IDLE) 
-                rt_usb_detach_instance(uhub->child[i]);
+            if(hub->child[i]->status != DEV_STATUS_IDLE) 
+                rt_usbh_detach_instance(hub->child[i]);
             
-            ret = rt_usb_hub_port_debounce(uhub, i + 1);
+            ret = rt_usbh_hub_port_debounce(hub, i + 1);
             if(ret != RT_EOK) continue;
             
             /* allocate an usb instance for new connected device */
-            uinst = rt_usb_alloc_instance();
-            if(uinst == RT_NULL) break;
+            device = rt_usbh_alloc_instance();
+            if(device == RT_NULL) break;
             
             /* set usb device speed */
-            uinst->speed = (pstatus & PORT_LSDA) ? 1 : 0;
-            uinst->parent = uhub;    
-            uinst->hcd = uhub->hcd;
-            uhub->child[i] = uinst;
+            device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
+            device->parent = hub;    
+            device->hcd = hub->hcd;
+            hub->child[i] = device;
 
             /* reset usb roothub port */
-            rt_usb_hub_reset_port(uhub, i + 1);
+            rt_usbh_hub_reset_port(hub, i + 1);
             
             /* attatch the usb instance to the hcd */
-            rt_usb_attatch_instance(uinst); 
+            rt_usbh_attatch_instance(device); 
         }
     }
 
@@ -363,18 +363,18 @@ static rt_err_t rt_usb_hub_port_change(uhubinst_t uhub)
  * 
  * @return none.
  */
-static void rt_usb_hub_irq(void* context)
+static void rt_usbh_hub_irq(void* context)
 {
     upipe_t pipe; 
-    uifinst_t ifinst;
-    uhubinst_t uhub;
+    struct uintf* intf;
+    uhub_t hub;
     int timeout = 100;
     
     RT_ASSERT(context != RT_NULL);
     
     pipe = (upipe_t)context;
-    ifinst = pipe->ifinst;
-    uhub = (uhubinst_t)ifinst->user_data;
+    intf = pipe->intf;
+    hub = (uhub_t)intf->user_data;
 
     if(pipe->status != UPIPE_STATUS_OK)
     {
@@ -382,14 +382,14 @@ static void rt_usb_hub_irq(void* context)
         return;
     }
     
-    rt_usb_hub_port_change(uhub);
+    rt_usbh_hub_port_change(hub);
 
     rt_kprintf("hub int xfer...\n");
 
     /* parameter check */
-     RT_ASSERT(pipe->ifinst->uinst->hcd != RT_NULL);
+     RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
     
-    rt_usb_hcd_int_xfer(ifinst->uinst->hcd, pipe, uhub->buffer, 
+    rt_usb_hcd_int_xfer(intf->device->hcd, pipe, hub->buffer, 
         pipe->ep.wMaxPacketSize, timeout);
 }
 
@@ -401,33 +401,33 @@ static void rt_usb_hub_irq(void* context)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-static rt_err_t rt_usb_hub_run(void *arg)
+static rt_err_t rt_usbh_hub_enable(void *arg)
 {
     int i = 0;
     rt_err_t ret = RT_EOK;
     uep_desc_t ep_desc;
-    uhubinst_t uhub;
-    uinst_t uinst;
-    uifinst_t ifinst = (uifinst_t)arg;
+    uhub_t hub;
+    struct uinstance* device;
+    struct uintf* intf = (struct uintf*)arg;
     int timeout = 300;
 
     /* paremeter check */
-    RT_ASSERT(ifinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
     
-    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_hub_run\n"));
+    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
 
     /* get usb device instance */
-    uinst = ifinst->uinst;
+    device = intf->device;
 
     /* create a hub instance */
-    uhub = rt_malloc(sizeof(struct uhubinst));
-    rt_memset(uhub, 0, sizeof(struct uhubinst));
+    hub = rt_malloc(sizeof(struct uhub));
+    rt_memset(hub, 0, sizeof(struct uhub));
     
     /* make interface instance's user data point to hub instance */
-    ifinst->user_data = (void*)uhub;
+    intf->user_data = (void*)hub;
 
     /* get hub descriptor head */
-    ret = rt_usb_hub_get_descriptor(uinst, (rt_uint8_t*)&uhub->hub_desc, 8);
+    ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
     if(ret != RT_EOK)
     {
         rt_kprintf("get hub descriptor failed\n");
@@ -435,8 +435,8 @@ static rt_err_t rt_usb_hub_run(void *arg)
     }
 
     /* get full hub descriptor */
-    ret = rt_usb_hub_get_descriptor(uinst, (rt_uint8_t*)&uhub->hub_desc, 
-        uhub->hub_desc.length);
+    ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 
+        hub->hub_desc.length);
     if(ret != RT_EOK)
     {
         rt_kprintf("get hub descriptor again failed\n");
@@ -444,23 +444,23 @@ static rt_err_t rt_usb_hub_run(void *arg)
     }    
 
     /* get hub ports number */
-    uhub->num_ports = uhub->hub_desc.num_ports;
-    uhub->hcd = uinst->hcd;
-    uhub->self = uinst;
+    hub->num_ports = hub->hub_desc.num_ports;
+    hub->hcd = device->hcd;
+    hub->self = device;
 
     /* reset all hub ports */
-    for (i = 0; i < uhub->num_ports; i++)
+    for (i = 0; i < hub->num_ports; i++)
     {
-        rt_usb_hub_set_port_feature(uhub, i + 1, PORT_FEAT_POWER);
-        rt_thread_delay(uhub->hub_desc.pwron_to_good
+        rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
+        rt_thread_delay(hub->hub_desc.pwron_to_good
             * 2 * RT_TICK_PER_SECOND / 1000 );
     }
 
-    if(ifinst->intf_desc->bNumEndpoints != 1) 
+    if(intf->intf_desc->bNumEndpoints != 1) 
         return -RT_ERROR;
 
     /* get endpoint descriptor from interface descriptor */
-    rt_usb_get_endpoint_descriptor(ifinst->intf_desc, 0, &ep_desc);
+    rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
     if(ep_desc == RT_NULL)
     {
         rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
@@ -474,17 +474,17 @@ static rt_err_t rt_usb_hub_run(void *arg)
         if(ep_desc->bEndpointAddress & USB_DIR_IN)
         {    
             /* allocate a pipe according to the endpoint type */
-            rt_usb_hcd_alloc_pipe(uinst->hcd, &uhub->pipe_in, ifinst, 
-                ep_desc, rt_usb_hub_irq);
+            rt_usb_hcd_alloc_pipe(device->hcd, &hub->pipe_in, intf, 
+                ep_desc, rt_usbh_hub_irq);
         }
         else return -RT_ERROR;
     }
 
     /* parameter check */
-     RT_ASSERT(uinst->hcd != RT_NULL);
+     RT_ASSERT(device->hcd != RT_NULL);
     
-    rt_usb_hcd_int_xfer(uinst->hcd, uhub->pipe_in, uhub->buffer, 
-        uhub->pipe_in->ep.wMaxPacketSize, timeout);
+    rt_usb_hcd_int_xfer(device->hcd, hub->pipe_in, hub->buffer, 
+        hub->pipe_in->ep.wMaxPacketSize, timeout);
     
     return RT_EOK;
 }
@@ -497,32 +497,32 @@ static rt_err_t rt_usb_hub_run(void *arg)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-static rt_err_t rt_usb_hub_stop(void* arg)
+static rt_err_t rt_usbh_hub_disable(void* arg)
 {
     int i;
-    uhubinst_t uhub;
-    uinst_t uinst;
-    uifinst_t ifinst = (uifinst_t)arg;
+    uhub_t hub;
+    struct uinstance* device;
+    struct uintf* intf = (struct uintf*)arg;
 
     /* paremeter check */
-    RT_ASSERT(ifinst != RT_NULL);
+    RT_ASSERT(intf != RT_NULL);
 
-    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_hub_stop\n"));
+    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
 
-    uinst = ifinst->uinst;
-    uhub = (uhubinst_t)ifinst->user_data;
+    device = intf->device;
+    hub = (uhub_t)intf->user_data;
 
-    if(uhub->pipe_in != RT_NULL)
-        rt_usb_hcd_free_pipe(uinst->hcd, uhub->pipe_in);
+    if(hub->pipe_in != RT_NULL)
+        rt_usb_hcd_free_pipe(device->hcd, hub->pipe_in);
 
-    for(i=0; i<uhub->num_ports; i++)
+    for(i=0; i<hub->num_ports; i++)
     {
-        if(uhub->child[i] != RT_NULL)
-            rt_usb_detach_instance(uhub->child[i]);
+        if(hub->child[i] != RT_NULL)
+            rt_usbh_detach_instance(hub->child[i]);
     }
     
-    if(uhub != RT_NULL) rt_free(uhub);
-    if(ifinst != RT_NULL) rt_free(ifinst);
+    if(hub != RT_NULL) rt_free(hub);
+    if(intf != RT_NULL) rt_free(intf);
 
     return RT_EOK;
 }
@@ -533,12 +533,12 @@ static rt_err_t rt_usb_hub_stop(void* arg)
  * 
  * @return the error code, RT_EOK on successfully.
  */
-ucd_t rt_usb_class_driver_hub(void)
+ucd_t rt_usbh_class_driver_hub(void)
 {    
     hub_driver.class_code = USB_CLASS_HUB;
     
-    hub_driver.run = rt_usb_hub_run;
-    hub_driver.stop = rt_usb_hub_stop;
+    hub_driver.enable = rt_usbh_hub_enable;
+    hub_driver.disable = rt_usbh_hub_disable;
 
     return &hub_driver;
 }
@@ -551,7 +551,7 @@ ucd_t rt_usb_class_driver_hub(void)
  * 
  * @return none.
  */
-static void rt_usb_hub_thread_entry(void* parameter)
+static void rt_usbh_hub_thread_entry(void* parameter)
 {    
     while(1)
     {    
@@ -566,7 +566,7 @@ static void rt_usb_hub_thread_entry(void* parameter)
         switch (msg.type)
         {        
         case USB_MSG_CONNECT_CHANGE:
-            rt_usb_hub_port_change(msg.content.uhub);
+            rt_usbh_hub_port_change(msg.content.hub);
             break;
         case USB_MSG_CALLBACK:
             /* invoke callback */
@@ -582,16 +582,15 @@ static void rt_usb_hub_thread_entry(void* parameter)
  * This function will post an message to the usb message queue,
  *
  * @param msg the message to be posted
- * @param size the size of the message .
  * 
  * @return the error code, RT_EOK on successfully. 
  */
-rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size)
+rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
 {
     RT_ASSERT(msg != RT_NULL);
 
     /* send message to usb message queue */
-    rt_mq_send(usb_mq, (void*)msg, size);
+    rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg));
 
     return RT_EOK;
 }
@@ -602,7 +601,7 @@ rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size)
  * @return none.
  * 
  */
-void rt_usb_hub_thread(void)
+void rt_usbh_hub_init(void)
 {
     rt_thread_t thread;
     
@@ -610,7 +609,7 @@ void rt_usb_hub_thread(void)
     usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
     
     /* create usb hub thread */
-    thread = rt_thread_create("usbh", rt_usb_hub_thread_entry, RT_NULL, 
+    thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL, 
         USB_THREAD_STACK_SIZE, 8, 20);
     if(thread != RT_NULL)
     {

+ 37 - 25
components/drivers/usb/usbhost/core/usbhost.c

@@ -24,7 +24,9 @@
 #include <rtthread.h>
 #include <drivers/usb_host.h>
 
-#if defined(RT_USB_HID_KEYBOARD) || defined(RT_USB_HID_MOUSE)
+#define USB_HOST_CONTROLLER_NAME      "usbh"
+
+#if defined(RT_USBH_HID_KEYBOARD) || defined(RT_USBH_HID_MOUSE)
 #include <hid.h>
 #endif
 
@@ -34,54 +36,64 @@
  * 
  * @return none.
  */
-int rt_usb_host_init(void)
+rt_err_t rt_usb_host_init(void)
 {
     ucd_t drv;
-#ifdef RT_USB_CLASS_HID
+    rt_device_t uhc;    
+#ifdef RT_USBH_HID
     uprotocal_t protocal;
 #endif
 
-    /* initialize usb hub thread */
-    rt_usb_hub_thread();
+    uhc = rt_device_find(USB_HOST_CONTROLLER_NAME);
+    if(uhc == RT_NULL)
+    {
+        rt_kprintf("can't find usb host controller %s\n", USB_HOST_CONTROLLER_NAME);
+        return -RT_ERROR;
+    }
+
+    /* initialize usb hub */
+    rt_usbh_hub_init();
 
     /* initialize class driver */
-    rt_usb_class_driver_init();
+    rt_usbh_class_driver_init();
 
-#ifdef RT_USB_CLASS_MASS_STORAGE
+#ifdef RT_USBH_MSTORAGE
     /* register mass storage class driver */
-    drv = rt_usb_class_driver_storage();
-    rt_usb_class_driver_register(drv);
+    drv = rt_usbh_class_driver_storage();
+    rt_usbh_class_driver_register(drv);
 #endif
 
-#ifdef RT_USB_CLASS_HID
+#ifdef RT_USBH_HID
     /* register hid class driver */
-    drv = rt_usb_class_driver_hid();
-    rt_usb_class_driver_register(drv);
+    drv = rt_usbh_class_driver_hid();
+    rt_usbh_class_driver_register(drv);
 
-#ifdef RT_USB_HID_KEYBOARD    
+#ifdef RT_USBH_HID_KEYBOARD    
     /* register hid keyboard protocal */
-    protocal = rt_usb_hid_protocal_kbd();    
-    rt_usb_hid_protocal_register(protocal);
+    protocal = rt_usbh_hid_protocal_kbd();    
+    rt_usbh_hid_protocal_register(protocal);
 #endif
 
-#ifdef RT_USB_HID_MOUSE    
+#ifdef RT_USBH_HID_MOUSE    
     /* register hid mouse protocal */
-    protocal = rt_usb_hid_protocal_mouse();    
-    rt_usb_hid_protocal_register(protocal);
+    protocal = rt_usbh_hid_protocal_mouse();    
+    rt_usbh_hid_protocal_register(protocal);
 #endif    
 #endif
 
-#ifdef RT_USB_CLASS_ADK
+#ifdef RT_USBH_ADK
     /* register adk class driver */
-    drv = rt_usb_class_driver_adk();
-    rt_usb_class_driver_register(drv);
+    drv = rt_usbh_class_driver_adk();
+    rt_usbh_class_driver_register(drv);
 #endif
 
     /* register hub class driver */
-    drv = rt_usb_class_driver_hub();
-    rt_usb_class_driver_register(drv);
+    drv = rt_usbh_class_driver_hub();
+    rt_usbh_class_driver_register(drv);
+
+    /* initialize usb host controller */
+    rt_device_init(uhc);
 
-	return 0;
+    return RT_EOK;
 }
-INIT_COMPONENT_EXPORT(rt_usb_host_init);