From 9b44af941f9aef0657c4df7600258e5efe80b010 Mon Sep 17 00:00:00 2001 From: uestczyh222 Date: Thu, 14 Dec 2017 22:45:12 +0800 Subject: [PATCH] [Components][Drivers][USB HOST]Fix core stack and msc driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 可以挂U盘了 欢迎测试鲁棒性已打通F4全系列HOST驱动(OTGFS) --- components/drivers/Kconfig | 9 +++- components/drivers/usb/usbhost/class/mass.c | 40 ++++++++------ components/drivers/usb/usbhost/class/udisk.c | 21 +++++--- components/drivers/usb/usbhost/core/core.c | 55 +++++++++++++------- components/drivers/usb/usbhost/core/hub.c | 4 +- 5 files changed, 82 insertions(+), 47 deletions(-) diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 7c1929b15f..5b95413b10 100644 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -107,9 +107,14 @@ menu "Using USB" default n if RT_USING_USB_HOST - config RT_USBH_ADK - bool "Enable connected with Android by ADK USB" + config RT_USBH_MSTORAGE + bool "Enable Udisk Drivers" default n + if RT_USBH_MSTORAGE + config UDISK_MOUNTPOINT + string "Udisk mount dir" + default "/" + endif endif config RT_USING_USB_DEVICE bool "Using USB device" diff --git a/components/drivers/usb/usbhost/class/mass.c b/components/drivers/usb/usbhost/class/mass.c index 2d29c63547..ff52f8d6f5 100644 --- a/components/drivers/usb/usbhost/class/mass.c +++ b/components/drivers/usb/usbhost/class/mass.c @@ -59,11 +59,14 @@ static rt_err_t _pipe_check(struct uhintf* intf, upipe_t pipe) rt_kprintf("pipe status error\n"); return -RT_EIO; } + if(pipe->status == UPIPE_STATUS_STALL) + { + /* clear the pipe stall status */ + ret = rt_usbh_clear_feature(device, pipe->ep.bEndpointAddress, + USB_FEATURE_ENDPOINT_HALT); + if(ret != RT_EOK) return ret; + } - /* clear the pipe stall status */ - ret = rt_usbh_clear_feature(device, pipe->ep.bEndpointAddress, - USB_FEATURE_ENDPOINT_HALT); - if(ret != RT_EOK) return ret; rt_thread_delay(50); @@ -172,7 +175,7 @@ static rt_err_t rt_usb_bulk_only_xfer(struct uhintf* intf, if(csw.status != 0) { - rt_kprintf("csw status error:%d\n",csw.status); + //rt_kprintf("csw status error:%d\n",csw.status); return -RT_ERROR; } @@ -211,16 +214,20 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun) USB_REQ_TYPE_INTERFACE; setup.bRequest = USBREQ_GET_MAX_LUN; setup.wValue = intf->intf_desc->bInterfaceNumber; - setup.wIndex = 1; - setup.wLength = 0; + setup.wIndex = 0; + setup.wLength = 1; /* do control transfer request */ 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, max_lun, 1, timeout) != 1) - { - return -RT_EIO; - } + return -RT_EIO; + } + if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, max_lun, 1, timeout) != 1) + { + return -RT_EIO; + } + if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) != 0) + { return -RT_EIO; } return RT_EOK; @@ -264,7 +271,10 @@ rt_err_t rt_usbh_storage_reset(struct uhintf* intf) { return -RT_EIO; } - + if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0) + { + return -RT_EIO; + } return RT_EOK; } @@ -462,7 +472,7 @@ rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer) cmd.xfer_len = 36; cmd.dflags = CBWFLAGS_DIR_IN; cmd.lun = 0; - cmd.cb_len = 12; + cmd.cb_len = 6;//12 cmd.cb[0] = SCSI_INQUIRY_CMD; cmd.cb[4] = 36; @@ -614,10 +624,6 @@ static rt_err_t rt_usbh_storage_disable(void* arg) /* free storage instance */ if(stor != RT_NULL) rt_free(stor); - - /* free interface instance */ - if(intf != RT_NULL) rt_free(intf); - return RT_EOK; } diff --git a/components/drivers/usb/usbhost/class/udisk.c b/components/drivers/usb/usbhost/class/udisk.c index e762f21734..6a4e02fa9c 100644 --- a/components/drivers/usb/usbhost/class/udisk.c +++ b/components/drivers/usb/usbhost/class/udisk.c @@ -141,7 +141,7 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff * * @return the error code, RT_EOK on successfully. */ -static rt_err_t rt_udisk_control(rt_device_t dev, rt_uint8_t cmd, void *args) +static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) { ustor_t stor; struct ustor_data* data; @@ -203,14 +203,21 @@ rt_err_t rt_udisk_run(struct uhintf* intf) rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT); /* reset pipe in endpoint */ - ret = rt_usbh_clear_feature(intf->device, + if(stor->pipe_in->status == UPIPE_STATUS_STALL) + { + ret = rt_usbh_clear_feature(intf->device, stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); - if(ret != RT_EOK) return ret; + if(ret != RT_EOK) return ret; + } + /* reset pipe out endpoint */ - ret = rt_usbh_clear_feature(intf->device, - stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); - if(ret != RT_EOK) return ret; + if(stor->pipe_out->status == UPIPE_STATUS_STALL) + { + ret = rt_usbh_clear_feature(intf->device, + stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); + if(ret != RT_EOK) return ret; + } while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK) { @@ -223,7 +230,7 @@ rt_err_t rt_udisk_run(struct uhintf* intf) } i = 0; - + /* wait device ready */ while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK) { diff --git a/components/drivers/usb/usbhost/core/core.c b/components/drivers/usb/usbhost/core/core.c index 482a368676..854c3815af 100644 --- a/components/drivers/usb/usbhost/core/core.c +++ b/components/drivers/usb/usbhost/core/core.c @@ -83,7 +83,7 @@ static struct uendpoint_descriptor ep0_out_desc = USB_DESC_TYPE_ENDPOINT, 0x00 | USB_DIR_OUT, USB_EP_ATTR_CONTROL, - 0x40, + 0x00, 0x00, }; static struct uendpoint_descriptor ep0_in_desc = @@ -93,7 +93,7 @@ static struct uendpoint_descriptor ep0_in_desc = USB_DESC_TYPE_ENDPOINT, 0x00 | USB_DIR_IN, USB_EP_ATTR_CONTROL, - 0x40, + 0x00, 0x00, }; rt_err_t rt_usbh_attatch_instance(uinst_t device) @@ -112,7 +112,10 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor)); dev_desc = &device->dev_desc; + /* alloc address 0 ep0 pipe*/ + ep0_out_desc.wMaxPacketSize = 8; + ep0_in_desc.wMaxPacketSize = 8; 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); @@ -125,38 +128,43 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) rt_kprintf("get device descriptor head failed\n"); return ret; } - //should reset bus + + /* reset bus */ rt_usbh_hub_reset_port(device->parent_hub, device->port); + rt_thread_delay(2); + rt_usbh_hub_clear_port_feature(device->parent_hub, i + 1, PORT_FEAT_C_CONNECTION); /* set device address */ 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) { rt_kprintf("set device address failed\n"); return ret; } - /* free true address ep0 pipe*/ + /* 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); + + /* set device max packet size */ + ep0_out_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0; + ep0_in_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0; + + /* alloc 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 */ - device->max_packet_size = device->dev_desc.bMaxPacketSize0; - - RT_DEBUG_LOG(1, ("get device descriptor length %d\n", + RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n", dev_desc->bLength)); /* get full device descriptor again */ - ret = rt_usbh_get_descriptor - (device, 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"); return ret; } - RT_DEBUG_LOG(1, ("Vendor ID 0x%x\n", dev_desc->idVendor)); - RT_DEBUG_LOG(1, ("Product ID 0x%x\n", dev_desc->idProduct)); + 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)); /* get configuration descriptor head */ ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18); @@ -195,7 +203,7 @@ rt_err_t rt_usbh_attatch_instance(uinst_t device) return -RT_ERROR; } - RT_DEBUG_LOG(1, ("interface class 0x%x, subclass 0x%x\n", + RT_DEBUG_LOG(RT_DEBUG_USB, ("interface class 0x%x, subclass 0x%x\n", intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass)); /* alloc pipe*/ @@ -275,14 +283,17 @@ rt_err_t rt_usbh_detach_instance(uinst_t device) 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_free(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) + while(device->pipe.next!= &device->pipe) { + l = device->pipe.next; + rt_list_remove(l); rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list)); } rt_memset(device, 0, sizeof(struct uinstance)); @@ -390,7 +401,10 @@ rt_err_t rt_usbh_set_configure(uinst_t device, int config) { return RT_ERROR; } - + if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0) + { + return RT_ERROR; + } return RT_EOK; } @@ -555,12 +569,15 @@ int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int rt_size_t remain_size; rt_size_t send_size; remain_size = nbytes; + rt_uint8_t * pbuffer = (rt_uint8_t *)buffer; do { + RT_DEBUG_LOG(RT_DEBUG_USB,("pipe transform remain size,: %d\n", remain_size)); 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) + if(hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, pbuffer, send_size, timeout) == send_size) { remain_size -= send_size; + pbuffer += send_size; } else { diff --git a/components/drivers/usb/usbhost/core/hub.c b/components/drivers/usb/usbhost/core/hub.c index 0f14acdd2d..56e40bb0d5 100644 --- a/components/drivers/usb/usbhost/core/hub.c +++ b/components/drivers/usb/usbhost/core/hub.c @@ -25,7 +25,7 @@ #include #include -#define USB_THREAD_STACK_SIZE 2048 +#define USB_THREAD_STACK_SIZE 4096 static struct rt_messagequeue *usb_mq; static struct uclass_driver hub_driver; @@ -481,7 +481,7 @@ static void rt_usbh_hub_irq(void* context) rt_usbh_hub_port_change(hub); - rt_kprintf(RT_DEBUG_USB,("hub int xfer...\n")); + RT_DEBUG_LOG(RT_DEBUG_USB,("hub int xfer...\n")); /* parameter check */ RT_ASSERT(pipe->inst->hcd != RT_NULL);