diff options
| author | Bernard Xiong <[email protected]> | 2018-06-10 17:59:17 +0800 |
|---|---|---|
| committer | Bernard Xiong <[email protected]> | 2018-06-10 17:59:17 +0800 |
| commit | 884fb70fe9973eaf01a7df04941cbcb0475ba8d5 (patch) | |
| tree | 7c8e3f42629e46b3d7ecb0c8507f67934d9fae47 /components/drivers/usb | |
| parent | bca65f30a90857287dc1371ddbc8d5f165691c7f (diff) | |
[Kernel] Add device ops feature.
Diffstat (limited to 'components/drivers/usb')
| -rw-r--r-- | components/drivers/usb/usbdevice/class/ecm.c | 13 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/hid.c | 21 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/rndis.c | 21 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/winusb.c | 18 | ||||
| -rw-r--r-- | components/drivers/usb/usbhost/class/adk.c | 178 | ||||
| -rw-r--r-- | components/drivers/usb/usbhost/class/udisk.c | 160 |
6 files changed, 257 insertions, 154 deletions
diff --git a/components/drivers/usb/usbdevice/class/ecm.c b/components/drivers/usb/usbdevice/class/ecm.c index 0711de3f51..6f19a0099e 100644 --- a/components/drivers/usb/usbdevice/class/ecm.c +++ b/components/drivers/usb/usbdevice/class/ecm.c @@ -327,6 +327,19 @@ static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args) return RT_EOK; } + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops ecm_device_ops = +{ + rt_ecm_eth_init, + rt_ecm_eth_open, + rt_ecm_eth_close, + rt_ecm_eth_read, + rt_ecm_eth_write, + rt_ecm_eth_control +}; +#endif + struct pbuf *rt_ecm_eth_rx(rt_device_t dev) { struct pbuf* p = RT_NULL; diff --git a/components/drivers/usb/usbdevice/class/hid.c b/components/drivers/usb/usbdevice/class/hid.c index 29e581d62b..fe1050e3ef 100644 --- a/components/drivers/usb/usbdevice/class/hid.c +++ b/components/drivers/usb/usbdevice/class/hid.c @@ -592,14 +592,33 @@ static void hid_thread_entry(void* parameter) HID_Report_Received(&report); } } + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops hid_device_ops = +{ + RT_NULL, + RT_NULL, + RT_NULL, + RT_NULL, + _hid_write, + RT_NULL, +}; +#endif + static rt_uint8_t hid_mq_pool[(sizeof(struct hid_report)+sizeof(void*))*8]; static void rt_usb_hid_init(struct ufunction *func) { struct hid_s *hiddev; hiddev = (struct hid_s *)func->user_data; rt_memset(&hiddev->parent, 0, sizeof(hiddev->parent)); + +#ifdef RT_USING_DEVICE_OPS + hiddev->parent.ops = &hid_device_ops; +#else hiddev->parent.write = _hid_write; - hiddev->func = func; +#endif + hiddev->func = func; + rt_device_register(&hiddev->parent, "hidd", RT_DEVICE_FLAG_RDWR); rt_mq_init(&hiddev->hid_mq, "hiddmq", hid_mq_pool, sizeof(struct hid_report), sizeof(hid_mq_pool), RT_IPC_FLAG_FIFO); diff --git a/components/drivers/usb/usbdevice/class/rndis.c b/components/drivers/usb/usbdevice/class/rndis.c index 7e67b28bef..0f680e9066 100644 --- a/components/drivers/usb/usbdevice/class/rndis.c +++ b/components/drivers/usb/usbdevice/class/rndis.c @@ -1180,6 +1180,19 @@ rt_err_t rt_rndis_eth_tx(rt_device_t dev, struct pbuf* p) return result; } + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops rndis_device_ops = +{ + rt_rndis_eth_init, + rt_rndis_eth_open, + rt_rndis_eth_close, + rt_rndis_eth_read, + rt_rndis_eth_write, + rt_rndis_eth_control +}; +#endif + #endif /* RT_USING_LWIP */ #ifdef RNDIS_DELAY_LINK_UP @@ -1307,20 +1320,24 @@ ufunction_t rt_usbd_function_rndis_create(udevice_t device) _rndis->host_addr[4] = 0xEA;//*(const rt_uint8_t *)(0x0FE081F1); _rndis->host_addr[5] = 0x13;//*(const rt_uint8_t *)(0x0FE081F2); +#ifdef RT_USING_DEVICE_OPS + _rndis->parent.parent.ops = &rndis_device_ops; +#else _rndis->parent.parent.init = rt_rndis_eth_init; _rndis->parent.parent.open = rt_rndis_eth_open; _rndis->parent.parent.close = rt_rndis_eth_close; _rndis->parent.parent.read = rt_rndis_eth_read; _rndis->parent.parent.write = rt_rndis_eth_write; _rndis->parent.parent.control = rt_rndis_eth_control; +#endif _rndis->parent.parent.user_data = device; _rndis->parent.eth_rx = rt_rndis_eth_rx; _rndis->parent.eth_tx = rt_rndis_eth_tx; - /* register eth device */ + /* register eth device */ eth_device_init(&((rt_rndis_eth_t)cdc->user_data)->parent, "u0"); - + #endif /* RT_USING_LWIP */ return cdc; diff --git a/components/drivers/usb/usbdevice/class/winusb.c b/components/drivers/usb/usbdevice/class/winusb.c index 71d946fcd9..9e7c957e7c 100644 --- a/components/drivers/usb/usbdevice/class/winusb.c +++ b/components/drivers/usb/usbdevice/class/winusb.c @@ -246,16 +246,34 @@ static rt_err_t win_usb_control(rt_device_t dev, int cmd, void *args) } return RT_EOK; } + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops winusb_device_ops = +{ + RT_NULL, + RT_NULL, + RT_NULL, + win_usb_read, + win_usb_write, + win_usb_control, +}; +#endif + static rt_err_t rt_usb_winusb_init(ufunction_t func) { winusb_device_t winusb_device = (winusb_device_t)func->user_data; winusb_device->parent.type = RT_Device_Class_Miscellaneous; + +#ifdef RT_USING_DEVICE_OPS + winusb_device->parent.ops = &winusb_device_ops; +#else winusb_device->parent.init = RT_NULL; winusb_device->parent.open = RT_NULL; winusb_device->parent.close = RT_NULL; winusb_device->parent.read = win_usb_read; winusb_device->parent.write = win_usb_write; winusb_device->parent.control = win_usb_control; +#endif winusb_device->parent.user_data = func; diff --git a/components/drivers/usb/usbhost/class/adk.c b/components/drivers/usb/usbhost/class/adk.c index 1481d27ea6..e7eed45127 100644 --- a/components/drivers/usb/usbhost/class/adk.c +++ b/components/drivers/usb/usbhost/class/adk.c @@ -37,7 +37,7 @@ static const char* _adk_uri = RT_NULL; static const char* _adk_serial = RT_NULL; 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* description, const char* _version, const char* uri, const char* serial) { _adk_manufacturer = manufacturer; @@ -62,31 +62,31 @@ RTM_EXPORT(rt_usbh_adk_set_string); * @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_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol) { struct urequest setup; - uinst_t device; + uinst_t device; int timeout = 100; - + /* parameter check */ RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf->device != RT_NULL); device = intf->device; - setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR | + setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_DEVICE; setup.request = USB_REQ_GET_PROTOCOL; setup.index = 0; setup.length = 2; setup.value = 0; - if(rt_usb_hcd_control_xfer(device->hcd, device, &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; + else return -RT_FALSE; } /** @@ -95,32 +95,32 @@ static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protoc * @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_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, +static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, const char* str) { struct urequest setup; - uinst_t device; + uinst_t device; int timeout = 100; - + /* parameter check */ RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf->device != RT_NULL); device = intf->device; - setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | + setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_DEVICE; setup.request = USB_REQ_SEND_STRING; setup.index = index; setup.length = rt_strlen(str) + 1; setup.value = 0; - if(rt_usb_hcd_control_xfer(device->hcd, device, &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; + else return -RT_FALSE; } /** @@ -129,41 +129,41 @@ static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, * @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_usbh_adk_start(struct uintf* intf) { struct urequest setup; - uinst_t device; + uinst_t device; int timeout = 100; - + /* parameter check */ RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf->device != RT_NULL); device = intf->device; - setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | + setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_DEVICE; setup.request = USB_REQ_START; setup.index = 0; setup.length = 0; setup.value = 0; - if(rt_usb_hcd_control_xfer(device->hcd, device, &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; + else return -RT_FALSE; } /** * This function will read data from usb adk device * * @param intf the interface instance. - * + * * @return the error code, RT_EOK on successfully. */ -static rt_size_t rt_usbh_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) { uadk_t adk; @@ -177,9 +177,9 @@ static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer intf = (struct uintf*)device->user_data; adk = (uadk_t)intf->user_data; - length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in, + length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in, buffer, size, 300); - + return length; } @@ -188,33 +188,45 @@ static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer * This function will write data to usb adk device * * @param intf the interface instance. - * + * * @return the error code, RT_EOK on successfully. */ -static rt_size_t rt_usbh_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) { uadk_t adk; rt_size_t length; struct uintf* intf; - RT_ASSERT(buffer != RT_NULL); + RT_ASSERT(buffer != RT_NULL); intf = (struct uintf*)device->user_data; adk = (uadk_t)intf->user_data; - length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out, + length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out, (void*)buffer, size, 300); - + return length; } +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops adk_device_ops = +{ + RT_NULL; + RT_NULL; + RT_NULL; + rt_usbh_adk_read; + rt_usbh_adk_write; + RT_NULL; +}; +#endif + /** * This function will run adk class driver when usb device is detected and identified * as a adk class device, it will continue the enumulate process. * * @param arg the argument. - * + * * @return the error code, RT_EOK on successfully. */ static rt_err_t rt_usbh_adk_enable(void* arg) @@ -224,8 +236,8 @@ static rt_err_t rt_usbh_adk_enable(void* arg) struct uintf* intf = (struct uintf*)arg; udev_desc_t dev_desc; rt_uint16_t protocol; - rt_err_t ret; - + rt_err_t ret; + /* parameter check */ if(intf == RT_NULL) { @@ -234,72 +246,72 @@ static rt_err_t rt_usbh_adk_enable(void* arg) } RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n")); - + dev_desc = &intf->device->dev_desc; - if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID && - (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID || + if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID && + (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID || dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID)) { if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR; - - RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n")); + + RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n")); } else { - RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n")); - + RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n")); + if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK) { rt_kprintf("rt_usbh_adk_get_protocol failed\n"); return ret; } - if(protocol != 1) + if(protocol != 1) { rt_kprintf("read protocol failed\n"); return -RT_ERROR; - } + } - rt_usbh_adk_send_string(intf, + rt_usbh_adk_send_string(intf, ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer); - rt_usbh_adk_send_string(intf, + rt_usbh_adk_send_string(intf, ACCESSORY_STRING_MODEL, _adk_model); - rt_usbh_adk_send_string(intf, + rt_usbh_adk_send_string(intf, ACCESSORY_STRING_DESCRIPTION, _adk_description); - rt_usbh_adk_send_string(intf, + rt_usbh_adk_send_string(intf, ACCESSORY_STRING_VERSION, _adk_version); - rt_usbh_adk_send_string(intf, - ACCESSORY_STRING_URI, _adk_uri); - rt_usbh_adk_send_string(intf, - ACCESSORY_STRING_SERIAL, _adk_serial); + rt_usbh_adk_send_string(intf, + ACCESSORY_STRING_URI, _adk_uri); + rt_usbh_adk_send_string(intf, + ACCESSORY_STRING_SERIAL, _adk_serial); RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer)); RT_DEBUG_LOG(RT_DEBUG_USB, ("model %s\n", _adk_model)); RT_DEBUG_LOG(RT_DEBUG_USB, ("description %s\n", _adk_description)); RT_DEBUG_LOG(RT_DEBUG_USB, ("version %s\n", _adk_version)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _adk_serial)); - + 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_usbh_adk_start(intf)) != RT_EOK) { rt_kprintf("rt_usbh_adk_start failed\n"); return ret; - } + } return RT_EOK; } - + adk = rt_malloc(sizeof(struct uadkinst)); RT_ASSERT(adk != RT_NULL); /* initilize the data structure */ - rt_memset(adk, 0, sizeof(struct uadkinst)); + rt_memset(adk, 0, sizeof(struct uadkinst)); intf->user_data = (void*)adk; for(i=0; i<intf->intf_desc->bNumEndpoints; i++) - { + { uep_desc_t ep_desc; - + /* get endpoint descriptor from interface descriptor */ rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc); if(ep_desc == RT_NULL) @@ -307,24 +319,24 @@ static rt_err_t rt_usbh_adk_enable(void* arg) rt_kprintf("rt_usb_get_endpoint_descriptor error\n"); return -RT_ERROR; } - - /* the endpoint type of adk class should be BULK */ + + /* the endpoint type of adk class should be BULK */ if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK) continue; - + /* allocate pipes according to the endpoint type */ if(ep_desc->bEndpointAddress & USB_DIR_IN) { /* allocate an in pipe for the adk instance */ - ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in, + 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(intf->device->hcd, &adk->pipe_out, - intf, 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; } } @@ -334,33 +346,37 @@ static rt_err_t rt_usbh_adk_enable(void* arg) { rt_kprintf("pipe error, unsupported device\n"); return -RT_ERROR; - } + } /* set configuration */ ret = rt_usbh_set_configure(intf->device, 1); if(ret != RT_EOK) return ret; /* register adk device */ - 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.type = RT_Device_Class_Char; +#ifdef RT_USING_DEVICE_OPS + adk->device.ops = &adk_device_ops; +#else + 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; +#endif adk->device.user_data = (void*)intf; rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR); - + return RT_EOK; } /** - * This function will be invoked when usb device plug out is detected and it would clean + * This function will be invoked when usb device plug out is detected and it would clean * and release all hub class related resources. * * @param arg the argument. - * + * * @return the error code, RT_EOK on successfully. */ static rt_err_t rt_usbh_adk_disable(void* arg) @@ -373,12 +389,12 @@ static rt_err_t rt_usbh_adk_disable(void* arg) RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n")); adk = (uadk_t)intf->user_data; - if(adk == RT_NULL) + if(adk == RT_NULL) { - rt_free(intf); + rt_free(intf); return RT_EOK; } - + if(adk->pipe_in != RT_NULL) rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in); @@ -389,11 +405,11 @@ static rt_err_t rt_usbh_adk_disable(void* arg) rt_device_unregister(&adk->device); /* free adk instance */ - if(adk != RT_NULL) + if(adk != RT_NULL) { rt_free(adk); } - + /* free interface instance */ rt_free(intf); @@ -403,13 +419,13 @@ static rt_err_t rt_usbh_adk_disable(void* arg) /** * This function will register adk class driver to the usb class driver manager. * and it should be invoked in the usb system initialization. - * + * * @return the error code, RT_EOK on successfully. */ ucd_t rt_usbh_class_driver_adk(void) { adk_driver.class_code = USB_CLASS_ADK; - + adk_driver.enable = rt_usbh_adk_enable; adk_driver.disable = rt_usbh_adk_disable; diff --git a/components/drivers/usb/usbhost/class/udisk.c b/components/drivers/usb/usbhost/class/udisk.c index f8f47890f4..cdd2551c96 100644 --- a/components/drivers/usb/usbhost/class/udisk.c +++ b/components/drivers/usb/usbhost/class/udisk.c @@ -31,7 +31,7 @@ static int udisk_get_id(void) if((_udisk_idset & (1 << i)) != 0) continue; else break; } - + /* it should not happen */ if(i == UDISK_MAX_COUNT) RT_ASSERT(0); @@ -43,14 +43,14 @@ static void udisk_free_id(int id) { RT_ASSERT(id < UDISK_MAX_COUNT) - _udisk_idset &= ~(1 << id); + _udisk_idset &= ~(1 << id); } /** * This function will initialize the udisk device * * @param dev the pointer of device driver structure - * + * * @return RT_EOK */ static rt_err_t rt_udisk_init(rt_device_t dev) @@ -68,7 +68,7 @@ static rt_err_t rt_udisk_init(rt_device_t dev) * * @return the actually read size on successful, otherwise negative returned. */ -static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, +static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { rt_err_t ret; @@ -81,18 +81,18 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, RT_ASSERT(buffer != RT_NULL); if(size > 4096) timeout = 800; - + data = (struct ustor_data*)dev->user_data; intf = data->intf; ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, pos, size, timeout); - + if (ret != RT_EOK) { rt_kprintf("usb mass_storage read failed\n"); return 0; } - + return size; } @@ -106,7 +106,7 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, * * @return the actually written size on successful, otherwise negative returned. */ -static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer, +static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { rt_err_t ret; @@ -129,7 +129,7 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff rt_kprintf("usb mass_storage write %d sector failed\n", size); return 0; } - + return size; } @@ -138,9 +138,9 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff * * @param intf the interface instance. * @param buffer the data buffer to save inquiry data - * + * * @return the error code, RT_EOK on successfully. - */ + */ static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) { ustor_t stor; @@ -164,14 +164,26 @@ static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) geometry->sector_count = stor->capicity[0]; } - return RT_EOK; + return RT_EOK; } +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops udisk_device_ops = +{ + rt_udisk_init, + RT_NULL, + RT_NULL, + rt_udisk_read, + rt_udisk_write, + rt_udisk_control +}; +#endif + /** * This function will run udisk driver when usb disk is detected. * * @param intf the usb interface instance. - * + * * @return the error code, RT_EOK on successfully. */ rt_err_t rt_udisk_run(struct uhintf* intf) @@ -179,10 +191,10 @@ rt_err_t rt_udisk_run(struct uhintf* intf) int i = 0; rt_err_t ret; char dname[4]; - char sname[8]; + char sname[8]; rt_uint8_t max_lun, *sector, sense[18], inquiry[36]; - struct dfs_partition part[MAX_PARTITION_COUNT]; - ustor_t stor; + struct dfs_partition part[MAX_PARTITION_COUNT]; + ustor_t stor; /* check parameter */ RT_ASSERT(intf != RT_NULL); @@ -193,40 +205,40 @@ rt_err_t rt_udisk_run(struct uhintf* intf) // rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT); /* reset mass storage class device */ ret = rt_usbh_storage_reset(intf); - if(ret != RT_EOK) return ret; + if(ret != RT_EOK) return ret; stor = (ustor_t)intf->user_data; - /* get max logic unit number */ + /* get max logic unit number */ ret = rt_usbh_storage_get_max_lun(intf, &max_lun); - if(ret != RT_EOK) + if(ret != RT_EOK) rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT); /* reset pipe in endpoint */ if(stor->pipe_in->status == UPIPE_STATUS_STALL) { - ret = rt_usbh_clear_feature(intf->device, + 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 */ if(stor->pipe_out->status == UPIPE_STATUS_STALL) { - ret = rt_usbh_clear_feature(intf->device, + ret = rt_usbh_clear_feature(intf->device, stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); - if(ret != RT_EOK) return ret; + if(ret != RT_EOK) return ret; } while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK) - { + { if(ret == -RT_EIO) return ret; - rt_thread_delay(5); + rt_thread_delay(5); if(i++ < 10) continue; rt_kprintf("rt_usbh_storage_inquiry error\n"); - return -RT_ERROR; + return -RT_ERROR; } i = 0; @@ -235,26 +247,26 @@ rt_err_t rt_udisk_run(struct uhintf* intf) while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK) { if(ret == -RT_EIO) return ret; - + ret = rt_usbh_storage_request_sense(intf, sense); if(ret == -RT_EIO) return ret; - - rt_thread_delay(10); + + rt_thread_delay(10); if(i++ < 10) continue; rt_kprintf("rt_usbh_storage_test_unit_ready error\n"); - return -RT_ERROR; + return -RT_ERROR; } i = 0; rt_memset(stor->capicity, 0, sizeof(stor->capicity)); /* get storage capacity */ - while((ret = rt_usbh_storage_get_capacity(intf, + while((ret = rt_usbh_storage_get_capacity(intf, (rt_uint8_t*)stor->capicity)) != RT_EOK) - { - if(ret == -RT_EIO) return ret; - + { + if(ret == -RT_EIO) return ret; + rt_thread_delay(50); if(i++ < 10) continue; @@ -262,16 +274,16 @@ rt_err_t rt_udisk_run(struct uhintf* intf) stor->capicity[1] = 0x200; rt_kprintf("rt_usbh_storage_get_capacity error\n"); - break; + break; } stor->capicity[0] = uswap_32(stor->capicity[0]); stor->capicity[1] = uswap_32(stor->capicity[1]); stor->capicity[0] += 1; - - RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n", + + RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n", stor->capicity[0], stor->capicity[1])); - + /* get the first sector to read partition table */ sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE); if (sector == RT_NULL) @@ -283,42 +295,46 @@ rt_err_t rt_udisk_run(struct uhintf* intf) rt_memset(sector, 0, SECTOR_SIZE); RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n")); - + /* get the partition table */ ret = rt_usbh_storage_read10(intf, sector, 0, 1, 500); if(ret != RT_EOK) { rt_kprintf("read parition table error\n"); - rt_free(sector); - return -RT_ERROR; + rt_free(sector); + return -RT_ERROR; } RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n")); - + for(i=0; i<MAX_PARTITION_COUNT; i++) - { + { /* get the first partition */ ret = dfs_filesystem_get_partition(&part[i], sector, i); if (ret == RT_EOK) - { - struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); + { + struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); rt_memset(data, 0, sizeof(struct ustor_data)); - data->intf = intf; + data->intf = intf; data->udisk_id = udisk_get_id(); rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i); rt_snprintf(sname, 8, "sem_ud%d", i); data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); /* register sdcard device */ - stor->dev[i].type = RT_Device_Class_Block; - stor->dev[i].init = rt_udisk_init; - stor->dev[i].read = rt_udisk_read; - stor->dev[i].write = rt_udisk_write; + stor->dev[i].type = RT_Device_Class_Block; +#ifdef RT_USING_DEVICE_OPS + stor->dev[i].ops = &udisk_device_ops; +#else + stor->dev[i].init = rt_udisk_init; + stor->dev[i].read = rt_udisk_read; + stor->dev[i].write = rt_udisk_write; stor->dev[i].control = rt_udisk_control; +#endif stor->dev[i].user_data = (void*)data; - rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR | + rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); stor->dev_cnt++; @@ -330,16 +346,16 @@ rt_err_t rt_udisk_run(struct uhintf* intf) else { RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i)); - } + } } else { if(i == 0) - { - struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); - rt_memset(data, 0, sizeof(struct ustor_data)); + { + struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); + rt_memset(data, 0, sizeof(struct ustor_data)); data->udisk_id = udisk_get_id(); - + /* there is no partition table */ data->part.offset = 0; data->part.size = 0; @@ -349,23 +365,27 @@ rt_err_t rt_udisk_run(struct uhintf* intf) rt_snprintf(dname, 7, "udisk%d", data->udisk_id); /* register sdcard device */ - stor->dev[0].type = RT_Device_Class_Block; - stor->dev[0].init = rt_udisk_init; - stor->dev[0].read = rt_udisk_read; - stor->dev[0].write = rt_udisk_write; + stor->dev[0].type = RT_Device_Class_Block; +#ifdef RT_USING_DEVICE_OPS + stor->dev[i].ops = &udisk_device_ops; +#else + stor->dev[0].init = rt_udisk_init; + stor->dev[0].read = rt_udisk_read; + stor->dev[0].write = rt_udisk_write; stor->dev[0].control = rt_udisk_control; +#endif stor->dev[0].user_data = (void*)data; rt_device_register(&stor->dev[0], dname, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); stor->dev_cnt++; - if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT, + if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT, "elm", 0, 0) == 0) - { + { rt_kprintf("Mount FAT on Udisk successful.\n"); - } + } else { rt_kprintf("Mount FAT on Udisk failed.\n"); @@ -382,31 +402,31 @@ rt_err_t rt_udisk_run(struct uhintf* intf) } /** - * This function will be invoked when usb disk plug out is detected and it would clean + * This function will be invoked when usb disk plug out is detected and it would clean * and release all udisk related resources. * * @param intf the usb interface instance. - * + * * @return the error code, RT_EOK on successfully. */ rt_err_t rt_udisk_stop(struct uhintf* intf) { int i; - ustor_t stor; + ustor_t stor; struct ustor_data* data; /* check parameter */ RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf->device != RT_NULL); - stor = (ustor_t)intf->user_data; + stor = (ustor_t)intf->user_data; RT_ASSERT(stor != RT_NULL); for(i=0; i<stor->dev_cnt; i++) { rt_device_t dev = &stor->dev[i]; data = (struct ustor_data*)dev->user_data; - + /* unmount filesystem */ dfs_unmount(UDISK_MOUNTPOINT); |
