|
@@ -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)
|
|
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)
|
|
*
|
|
*
|
|
* @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)
|
|
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)
|
|
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)
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
- RT_DEBUG_LOG(RT_DEBUG_USB, ("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, ("Vendor ID 0x%x\n", dev_desc->idVendor));
|
|
|
|
+ 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)
|
|
|
|
|
|
/* 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)
|
|
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)
|
|
if(drv != RT_NULL)
|
|
if(drv != RT_NULL)
|
|
{
|
|
{
|
|
/* allocate memory for interface device */
|
|
/* allocate memory for interface device */
|
|
- device->intf[i] =
|
|
|
|
- (struct uintf*)rt_malloc(sizeof(struct uintf));
|
|
|
|
-
|
|
|
|
|
|
+ device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
|
|
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)
|
|
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)
|
|
}
|
|
}
|
|
|
|
|
|
/* 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)
|
|
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,
|
|
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,
|
|
|
|
- timeout) != nbytes) return -RT_EIO;
|
|
|
|
- else return RT_EOK;
|
|
|
|
|
|
+ if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
+ {
|
|
|
|
+ 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)
|
|
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,
|
|
|
|
- timeout) != 0) return -RT_EIO;
|
|
|
|
-
|
|
|
|
- rt_thread_delay(50);
|
|
|
|
|
|
+ if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
|
|
|
+ {
|
|
|
|
+ return RT_ERROR;
|
|
|
|
+ }
|
|
|
|
+ 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)
|
|
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,
|
|
|
|
- timeout) != 0) return -RT_EIO;
|
|
|
|
|
|
+ if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
|
|
|
+ {
|
|
|
|
+ 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)
|
|
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,
|
|
|
|
- timeout) != 0) return -RT_EIO;
|
|
|
|
|
|
+ if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
|
|
|
+ {
|
|
|
|
+ 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)
|
|
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,
|
|
|
|
- timeout) != 0) return -RT_EIO;
|
|
|
|
|
|
+ if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
|
|
|
+ {
|
|
|
|
+ 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,
|
|
/* 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,
|
|
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;
|
|
|
|
+}
|