rt-thread-official/components/drivers/usb/usbhost/class/adk.c

423 lines
11 KiB
C
Raw Normal View History

2013-01-08 22:40:58 +08:00
/*
2021-03-08 18:19:04 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2013-01-08 22:40:58 +08:00
*
2018-10-14 19:37:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2013-01-08 22:40:58 +08:00
*
* Change Logs:
* Date Author Notes
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "adk.h"
#ifdef RT_USBH_ADK
2013-01-08 22:40:58 +08:00
static struct uclass_driver adk_driver;
static const char* _adk_manufacturer = RT_NULL;
static const char* _adk_model = RT_NULL;
static const char* _adk_description = RT_NULL;
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_usbh_adk_set_string(const char* manufacturer, const char* model,
2018-06-10 17:59:17 +08:00
const char* description, const char* _version, const char* uri,
2013-01-08 22:40:58 +08:00
const char* serial)
{
_adk_manufacturer = manufacturer;
_adk_model = model;
_adk_description = description;
_adk_version = _version;
_adk_uri = uri;
_adk_serial = serial;
return RT_EOK;
}
#ifdef RT_USING_MODULE
#include <rtm.h>
RTM_EXPORT(rt_usbh_adk_set_string);
2013-01-08 22:40:58 +08:00
#endif
/**
* This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
*
* @param intf the interface instance.
2013-01-08 22:40:58 +08:00
* @duration the idle period of requesting data.
* @report_id the report id
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
2013-01-08 22:40:58 +08:00
{
2015-03-19 16:52:28 +08:00
struct urequest setup;
2018-06-10 17:59:17 +08:00
uinst_t device;
int timeout = USB_TIMEOUT_BASIC;
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* parameter check */
RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL);
2013-01-08 22:40:58 +08:00
device = intf->device;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR |
2013-01-08 22:40:58 +08:00
USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_GET_PROTOCOL;
setup.index = 0;
setup.length = 2;
setup.value = 0;
2018-06-10 17:59:17 +08:00
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2,
2013-01-08 22:40:58 +08:00
timeout) == 0) return RT_EOK;
2018-06-10 17:59:17 +08:00
else return -RT_FALSE;
2013-01-08 22:40:58 +08:00
}
/**
* This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
*
* @param intf the interface instance.
2013-01-08 22:40:58 +08:00
* @duration the idle period of requesting data.
* @report_id the report id
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
2018-06-10 17:59:17 +08:00
static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index,
2013-01-08 22:40:58 +08:00
const char* str)
{
2015-03-19 16:52:28 +08:00
struct urequest setup;
2018-06-10 17:59:17 +08:00
uinst_t device;
int timeout = USB_TIMEOUT_BASIC;
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* parameter check */
RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL);
2013-01-08 22:40:58 +08:00
device = intf->device;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
2013-01-08 22:40:58 +08:00
USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_SEND_STRING;
setup.index = index;
setup.length = rt_strlen(str) + 1;
setup.value = 0;
2018-06-10 17:59:17 +08:00
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str,
2013-01-08 22:40:58 +08:00
rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
2018-06-10 17:59:17 +08:00
else return -RT_FALSE;
2013-01-08 22:40:58 +08:00
}
/**
* This function will do USB_REQ_START request to set idle period to the usb adk device
*
* @param intf the interface instance.
2013-01-08 22:40:58 +08:00
* @duration the idle period of requesting data.
* @report_id the report id
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_adk_start(struct uintf* intf)
2013-01-08 22:40:58 +08:00
{
2015-03-19 16:52:28 +08:00
struct urequest setup;
2018-06-10 17:59:17 +08:00
uinst_t device;
int timeout = USB_TIMEOUT_BASIC;
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* parameter check */
RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL);
2013-01-08 22:40:58 +08:00
device = intf->device;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
2013-01-08 22:40:58 +08:00
USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_START;
setup.index = 0;
setup.length = 0;
setup.value = 0;
2018-06-10 17:59:17 +08:00
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
2013-01-08 22:40:58 +08:00
timeout) == 0) return RT_EOK;
2018-06-10 17:59:17 +08:00
else return -RT_FALSE;
2013-01-08 22:40:58 +08:00
}
/**
* This function will read data from usb adk device
*
* @param intf the interface instance.
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
2018-06-10 17:59:17 +08:00
static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
2013-01-08 22:40:58 +08:00
rt_size_t size)
{
uadk_t adk;
2013-01-08 22:40:58 +08:00
rt_size_t length;
struct uintf* intf;
2013-01-08 22:40:58 +08:00
/* check parameter */
RT_ASSERT(device != RT_NULL);
RT_ASSERT(buffer != RT_NULL);
intf = (struct uintf*)device->user_data;
adk = (uadk_t)intf->user_data;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in,
2013-01-08 22:40:58 +08:00
buffer, size, 300);
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
return length;
}
/**
* This function will write data to usb adk device
*
* @param intf the interface instance.
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
2018-06-10 17:59:17 +08:00
static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer,
2013-01-08 22:40:58 +08:00
rt_size_t size)
{
uadk_t adk;
2013-01-08 22:40:58 +08:00
rt_size_t length;
struct uintf* intf;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
RT_ASSERT(buffer != RT_NULL);
2013-01-08 22:40:58 +08:00
intf = (struct uintf*)device->user_data;
adk = (uadk_t)intf->user_data;
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out,
2013-01-08 22:40:58 +08:00
(void*)buffer, size, 300);
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
return length;
}
2018-06-10 17:59:17 +08:00
#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
2013-01-08 22:40:58 +08:00
/**
* 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.
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_adk_enable(void* arg)
2013-01-08 22:40:58 +08:00
{
int i = 0;
uadk_t adk;
struct uintf* intf = (struct uintf*)arg;
2013-01-08 22:40:58 +08:00
udev_desc_t dev_desc;
rt_uint16_t protocol;
2018-06-10 17:59:17 +08:00
rt_err_t ret;
2013-01-08 22:40:58 +08:00
/* parameter check */
if(intf == RT_NULL)
2013-01-08 22:40:58 +08:00
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n"));
2018-06-10 17:59:17 +08:00
dev_desc = &intf->device->dev_desc;
2018-06-10 17:59:17 +08:00
if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID &&
(dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID ||
2013-01-08 22:40:58 +08:00
dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
{
if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
2018-06-10 17:59:17 +08:00
RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n"));
2013-01-08 22:40:58 +08:00
}
else
{
2018-06-10 17:59:17 +08:00
RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n"));
if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK)
2013-01-08 22:40:58 +08:00
{
rt_kprintf("rt_usbh_adk_get_protocol failed\n");
2013-01-08 22:40:58 +08:00
return ret;
}
2018-06-10 17:59:17 +08:00
if(protocol != 1)
2013-01-08 22:40:58 +08:00
{
rt_kprintf("read protocol failed\n");
return -RT_ERROR;
2018-06-10 17:59:17 +08:00
}
2013-01-08 22:40:58 +08:00
2018-06-10 17:59:17 +08:00
rt_usbh_adk_send_string(intf,
2013-01-08 22:40:58 +08:00
ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
2018-06-10 17:59:17 +08:00
rt_usbh_adk_send_string(intf,
2013-01-08 22:40:58 +08:00
ACCESSORY_STRING_MODEL, _adk_model);
2018-06-10 17:59:17 +08:00
rt_usbh_adk_send_string(intf,
2013-01-08 22:40:58 +08:00
ACCESSORY_STRING_DESCRIPTION, _adk_description);
2018-06-10 17:59:17 +08:00
rt_usbh_adk_send_string(intf,
2013-01-08 22:40:58 +08:00
ACCESSORY_STRING_VERSION, _adk_version);
2018-06-10 17:59:17 +08:00
rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_URI, _adk_uri);
rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_SERIAL, _adk_serial);
2013-01-08 22:40:58 +08:00
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));
2018-06-10 17:59:17 +08:00
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)
2013-01-08 22:40:58 +08:00
{
rt_kprintf("rt_usbh_adk_start failed\n");
2013-01-08 22:40:58 +08:00
return ret;
2018-06-10 17:59:17 +08:00
}
2013-01-08 22:40:58 +08:00
return RT_EOK;
}
2018-06-10 17:59:17 +08:00
adk = rt_malloc(sizeof(struct uadkinst));
RT_ASSERT(adk != RT_NULL);
2013-01-08 22:40:58 +08:00
/* initilize the data structure */
2018-06-10 17:59:17 +08:00
rt_memset(adk, 0, sizeof(struct uadkinst));
intf->user_data = (void*)adk;
2013-01-08 22:40:58 +08:00
for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
2018-06-10 17:59:17 +08:00
{
2013-01-08 22:40:58 +08:00
uep_desc_t ep_desc;
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* get endpoint descriptor from interface descriptor */
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
2013-01-08 22:40:58 +08:00
if(ep_desc == RT_NULL)
{
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
return -RT_ERROR;
}
2018-06-10 17:59:17 +08:00
/* the endpoint type of adk class should be BULK */
2013-01-08 22:40:58 +08:00
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
continue;
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* allocate pipes according to the endpoint type */
if(ep_desc->bEndpointAddress & USB_DIR_IN)
{
/* allocate an in pipe for the adk instance */
2018-06-10 17:59:17 +08:00
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in,
intf, ep_desc, RT_NULL);
2013-01-08 22:40:58 +08:00
if(ret != RT_EOK) return ret;
}
else
2018-06-10 17:59:17 +08:00
{
2013-01-08 22:40:58 +08:00
/* allocate an output pipe for the adk instance */
2018-06-10 17:59:17 +08:00
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out,
intf, ep_desc, RT_NULL);
2013-01-08 22:40:58 +08:00
if(ret != RT_EOK) return ret;
}
}
/* check pipes infomation */
if(adk->pipe_in == RT_NULL || adk->pipe_out == RT_NULL)
2013-01-08 22:40:58 +08:00
{
rt_kprintf("pipe error, unsupported device\n");
return -RT_ERROR;
2018-06-10 17:59:17 +08:00
}
2013-01-08 22:40:58 +08:00
/* set configuration */
ret = rt_usbh_set_configure(intf->device, 1);
2013-01-08 22:40:58 +08:00
if(ret != RT_EOK) return ret;
/* register adk device */
2018-06-10 17:59:17 +08:00
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;
2018-06-10 17:59:17 +08:00
#endif
adk->device.user_data = (void*)intf;
rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
return RT_EOK;
}
/**
2018-06-10 17:59:17 +08:00
* This function will be invoked when usb device plug out is detected and it would clean
2013-01-08 22:40:58 +08:00
* and release all hub class related resources.
*
* @param arg the argument.
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_adk_disable(void* arg)
2013-01-08 22:40:58 +08:00
{
uadk_t adk;
struct uintf* intf = (struct uintf*)arg;
2013-01-08 22:40:58 +08:00
RT_ASSERT(intf != RT_NULL);
2013-01-08 22:40:58 +08:00
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n"));
2013-01-08 22:40:58 +08:00
adk = (uadk_t)intf->user_data;
2018-06-10 17:59:17 +08:00
if(adk == RT_NULL)
2013-01-08 22:40:58 +08:00
{
2018-06-10 17:59:17 +08:00
rt_free(intf);
2013-01-08 22:40:58 +08:00
return RT_EOK;
}
2018-06-10 17:59:17 +08:00
if(adk->pipe_in != RT_NULL)
rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
2013-01-08 22:40:58 +08:00
if(adk->pipe_out != RT_NULL)
rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_out);
2013-01-08 22:40:58 +08:00
/* unregister adk device */
rt_device_unregister(&adk->device);
2013-01-08 22:40:58 +08:00
/* free adk instance */
2018-06-10 17:59:17 +08:00
if(adk != RT_NULL)
{
rt_free(adk);
}
2018-06-10 17:59:17 +08:00
2013-01-08 22:40:58 +08:00
/* free interface instance */
rt_free(intf);
2013-01-08 22:40:58 +08:00
return RT_EOK;
}
/**
* This function will register adk class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization.
2018-06-10 17:59:17 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
ucd_t rt_usbh_class_driver_adk(void)
2013-01-08 22:40:58 +08:00
{
adk_driver.class_code = USB_CLASS_ADK;
2018-06-10 17:59:17 +08:00
adk_driver.enable = rt_usbh_adk_enable;
adk_driver.disable = rt_usbh_adk_disable;
2013-01-08 22:40:58 +08:00
return &adk_driver;
}
#endif