Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
507
sdk/lib/bus/rttusb/include/rttusb_device.h
Normal file
507
sdk/lib/bus/rttusb/include/rttusb_device.h
Normal file
@@ -0,0 +1,507 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
* 2012-12-12 heyuanjie87 change endpoint and function handler
|
||||
* 2013-04-26 aozima add DEVICEQUALIFIER support.
|
||||
* 2017-11-15 ZYH fix ep0 transform error
|
||||
*/
|
||||
|
||||
#ifndef __USB_DEVICE_H__
|
||||
#define __USB_DEVICE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "include/usb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef RT_USB_DEVICE_COMPOSITE
|
||||
#ifdef RT_USB_DEVICE_VIDEO
|
||||
#define RT_USB_DEVICE_COMPOSITE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Vendor ID */
|
||||
#ifdef USB_VENDOR_ID
|
||||
#define _VENDOR_ID USB_VENDOR_ID
|
||||
#else
|
||||
#define _VENDOR_ID 0xA012
|
||||
#endif
|
||||
/* Product ID */
|
||||
#ifdef USB_PRODUCT_ID
|
||||
#define _PRODUCT_ID USB_PRODUCT_ID
|
||||
#else
|
||||
#define _PRODUCT_ID 0x8001
|
||||
#endif
|
||||
|
||||
#ifndef MAX_INTF_STR
|
||||
#define MAX_INTF_STR 20
|
||||
#endif
|
||||
|
||||
#define USB_BCD_DEVICE 0x0200 /* USB Specification Release Number in Binary-Coded Decimal */
|
||||
#define USB_BCD_VERSION 0x0200 /* USB 2.0 */
|
||||
#define EP0_IN_ADDR 0x80
|
||||
#define EP0_OUT_ADDR 0x00
|
||||
#define EP_HANDLER(ep, func, size) RT_ASSERT(ep != RT_NULL); ep->handler(func, size)
|
||||
#define EP_ADDRESS(ep) ep->ep_desc->bEndpointAddress
|
||||
#define EP_MAXPACKET(ep) ep->ep_desc->wMaxPacketSize
|
||||
#define FUNC_ENABLE(func) do{ \
|
||||
if(func->ops->enable != RT_NULL && \
|
||||
func->enabled == RT_FALSE) \
|
||||
{ \
|
||||
if(func->ops->enable(func) == RT_EOK) \
|
||||
func->enabled = RT_TRUE; \
|
||||
} \
|
||||
}while(0)
|
||||
#define FUNC_DISABLE(func) do{ \
|
||||
if(func->ops->disable != RT_NULL && \
|
||||
func->enabled == RT_TRUE) \
|
||||
{ \
|
||||
func->enabled = RT_FALSE; \
|
||||
func->ops->disable(func); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
#define RT_USBD_CLASS_CTRL_CONNECTED (RT_DEVICE_CTRL_BASE(USBDevice) + 0)
|
||||
|
||||
struct ufunction;
|
||||
struct udevice;
|
||||
struct uendpoint;
|
||||
struct udevice;
|
||||
struct uconfig;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
/* request to read full count */
|
||||
UIO_REQUEST_READ_FULL,
|
||||
/* request to read any count */
|
||||
UIO_REQUEST_READ_BEST,
|
||||
/* request to write full count */
|
||||
UIO_REQUEST_WRITE,
|
||||
}UIO_REQUEST_TYPE;
|
||||
|
||||
struct udcd_ops
|
||||
{
|
||||
rt_err_t (*set_address)(rt_uint8_t address);
|
||||
rt_err_t (*set_config)(rt_uint8_t address);
|
||||
rt_err_t (*ep_set_stall)(rt_uint8_t address);
|
||||
rt_err_t (*ep_clear_stall)(rt_uint8_t address);
|
||||
rt_err_t (*ep_enable)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_disable)(struct uendpoint* ep);
|
||||
rt_size_t (*ep_read_prepare)(rt_uint8_t address, void *buffer, rt_size_t size);
|
||||
rt_size_t (*ep_read)(rt_uint8_t address, void *buffer);
|
||||
rt_size_t (*ep_write)(rt_uint8_t address, void *buffer, rt_size_t size);
|
||||
rt_err_t (*ep0_send_status)(void);
|
||||
rt_err_t (*suspend)(void);
|
||||
rt_err_t (*wakeup)(void);
|
||||
};
|
||||
|
||||
struct ep_id
|
||||
{
|
||||
rt_uint8_t addr;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t dir;
|
||||
rt_uint16_t maxpacket;
|
||||
rt_uint8_t status;
|
||||
};
|
||||
|
||||
typedef rt_err_t (*udep_handler_t)(struct ufunction* func, rt_size_t size);
|
||||
|
||||
struct uio_request
|
||||
{
|
||||
rt_list_t list;
|
||||
UIO_REQUEST_TYPE req_type;
|
||||
rt_uint8_t* buffer;
|
||||
rt_size_t size;
|
||||
rt_size_t remain_size;
|
||||
};
|
||||
typedef struct uio_request* uio_request_t;
|
||||
|
||||
struct uendpoint
|
||||
{
|
||||
rt_list_t list;
|
||||
uep_desc_t ep_desc;
|
||||
rt_list_t request_list;
|
||||
struct uio_request request;
|
||||
rt_uint8_t* buffer;
|
||||
rt_bool_t stalled;
|
||||
struct ep_id* id;
|
||||
udep_handler_t handler;
|
||||
rt_err_t (*rx_indicate)(struct udevice* dev, rt_size_t size);
|
||||
};
|
||||
typedef struct uendpoint* uep_t;
|
||||
|
||||
struct udcd
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct udcd_ops* ops;
|
||||
struct uendpoint ep0;
|
||||
uep0_stage_t stage;
|
||||
struct ep_id* ep_pool;
|
||||
rt_uint8_t device_is_hs;
|
||||
struct udevice* p_udevice;
|
||||
struct uconfig* cfg;
|
||||
rt_thread_t usb_thread;
|
||||
struct rt_messagequeue usb_mq;
|
||||
|
||||
};
|
||||
typedef struct udcd* udcd_t;
|
||||
|
||||
struct ualtsetting
|
||||
{
|
||||
rt_list_t list;
|
||||
uintf_desc_t intf_desc;
|
||||
void* desc;
|
||||
rt_size_t desc_size;
|
||||
rt_list_t ep_list;
|
||||
};
|
||||
typedef struct ualtsetting* ualtsetting_t;
|
||||
|
||||
typedef rt_err_t (*uintf_handler_t)(struct ufunction* func, ureq_t setup);
|
||||
|
||||
struct uinterface
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t intf_num;
|
||||
ualtsetting_t curr_setting;
|
||||
rt_list_t setting_list;
|
||||
uintf_handler_t handler;
|
||||
};
|
||||
typedef struct uinterface* uintf_t;
|
||||
|
||||
struct ufunction_ops
|
||||
{
|
||||
rt_err_t (*enable)(struct ufunction* func);
|
||||
rt_err_t (*disable)(struct ufunction* func);
|
||||
rt_err_t (*sof_handler)(struct ufunction* func);
|
||||
};
|
||||
typedef struct ufunction_ops* ufunction_ops_t;
|
||||
|
||||
struct ufunction
|
||||
{
|
||||
rt_list_t list;
|
||||
ufunction_ops_t ops;
|
||||
struct udevice* device;
|
||||
udev_desc_t dev_desc;
|
||||
void* user_data;
|
||||
rt_bool_t enabled;
|
||||
|
||||
rt_list_t intf_list;
|
||||
|
||||
uintf_t intf[3];
|
||||
ualtsetting_t setting[3];
|
||||
uep_t ep[6];
|
||||
|
||||
|
||||
};
|
||||
typedef struct ufunction* ufunction_t;
|
||||
|
||||
struct uconfig
|
||||
{
|
||||
rt_list_t list;
|
||||
struct uconfig_descriptor cfg_desc;
|
||||
rt_list_t func_list;
|
||||
};
|
||||
typedef struct uconfig* uconfig_t;
|
||||
|
||||
struct udevice
|
||||
{
|
||||
rt_list_t list;
|
||||
struct udevice_descriptor dev_desc;
|
||||
|
||||
struct usb_qualifier_descriptor * dev_qualifier;
|
||||
usb_os_comp_id_desc_t os_comp_id_desc;
|
||||
const char** str;
|
||||
const char *str_intf[MAX_INTF_STR];
|
||||
udevice_state_t state;
|
||||
rt_list_t cfg_list;
|
||||
uconfig_t curr_cfg;
|
||||
rt_uint8_t nr_intf;
|
||||
|
||||
udcd_t dcd;
|
||||
};
|
||||
typedef struct udevice* udevice_t;
|
||||
|
||||
struct udclass
|
||||
{
|
||||
rt_list_t list;
|
||||
ufunction_t (*rt_usbd_function_create)(udevice_t device);
|
||||
void (*rt_usbd_function_delete)();
|
||||
};
|
||||
typedef struct udclass* udclass_t;
|
||||
|
||||
enum udev_msg_type
|
||||
{
|
||||
USB_MSG_SETUP_NOTIFY,
|
||||
USB_MSG_DATA_NOTIFY,
|
||||
USB_MSG_EP0_OUT,
|
||||
USB_MSG_EP_CLEAR_FEATURE,
|
||||
USB_MSG_SOF,
|
||||
USB_MSG_RESET,
|
||||
USB_MSG_PLUG_IN,
|
||||
/* we don't need to add a "PLUG_IN" event because after the cable is
|
||||
* plugged in(before any SETUP) the classed have nothing to do. If the host
|
||||
* is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
|
||||
* should reset and run the class while plug_in is not. */
|
||||
USB_MSG_PLUG_OUT,
|
||||
};
|
||||
typedef enum udev_msg_type udev_msg_type;
|
||||
|
||||
struct ep_msg
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_uint8_t ep_addr;
|
||||
};
|
||||
|
||||
struct udev_msg
|
||||
{
|
||||
udev_msg_type type;
|
||||
udcd_t dcd;
|
||||
union
|
||||
{
|
||||
struct ep_msg ep_msg;
|
||||
struct urequest setup;
|
||||
} content;
|
||||
};
|
||||
typedef struct udev_msg* udev_msg_t;
|
||||
|
||||
int rt_usbd_class_list_init(rt_uint32_t devid);
|
||||
int rt_usbd_class_list_deinit(rt_uint32_t devid);
|
||||
udevice_t rt_usbd_device_new(void);
|
||||
uconfig_t rt_usbd_config_new(void);
|
||||
ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
|
||||
ufunction_ops_t ops);
|
||||
rt_err_t rt_usbd_function_release(ufunction_t func);
|
||||
uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler);
|
||||
uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler);
|
||||
ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size);
|
||||
|
||||
rt_err_t rt_usbd_core_device_list_init();
|
||||
rt_err_t rt_usbd_core_init(const char *dev, void **thread_t, void *usb_mq);
|
||||
rt_err_t rt_usbd_core_deinit(void **thread_t, void *usb_mq);
|
||||
rt_err_t rt_usb_device_init(rt_uint32_t devid, const char *dev);
|
||||
rt_err_t rt_usb_device_deinit(rt_uint32_t devid);
|
||||
rt_err_t rt_usbd_event_signal(struct udev_msg* msg);
|
||||
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd);
|
||||
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc);
|
||||
rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
|
||||
rt_err_t rt_usbd_device_set_interface_string(udevice_t device, int index, const char* string);
|
||||
rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier);
|
||||
rt_err_t rt_usbd_device_set_os_comp_id_desc(udevice_t device, usb_os_comp_id_desc_t os_comp_id_desc);
|
||||
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg);
|
||||
rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func);
|
||||
rt_err_t rt_usbd_class_register(udclass_t udclass, rt_uint32_t devid);
|
||||
rt_err_t rt_usbd_class_driver_unregister(udclass_t udclass, rt_uint32_t devid);
|
||||
udclass_t rt_usbd_class_driver_find(udclass_t udclass, rt_uint32_t devid);
|
||||
rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf);
|
||||
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting);
|
||||
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep);
|
||||
rt_err_t rt_usbd_os_comp_id_desc_add_os_func_comp_id_desc(usb_os_comp_id_desc_t os_comp_id_desc, usb_os_func_comp_id_desc_t os_func_comp_id_desc);
|
||||
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos);
|
||||
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value);
|
||||
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value);
|
||||
|
||||
udevice_t rt_usbd_find_device(udcd_t dcd);
|
||||
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value);
|
||||
uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc);
|
||||
uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr);
|
||||
rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req);
|
||||
rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size);
|
||||
rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
|
||||
rt_err_t (*rx_ind)(udevice_t device, rt_size_t size));
|
||||
|
||||
int audio_speaker_init(void);
|
||||
int rt_usbd_uac_speaker_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_uac_mic_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_uvc_device_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_vcom_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_ecm_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_hid_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_msc_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_rndis_class_register(rt_uint32_t devid);
|
||||
int rt_usbd_winusb_class_register(rt_uint32_t devid);
|
||||
|
||||
int rt_usbd_vcom_class_unregister(rt_uint32_t devid);
|
||||
int rt_usbd_msc_class_unregister(rt_uint32_t devid);
|
||||
|
||||
#ifdef RT_USB_DEVICE_COMPOSITE
|
||||
rt_err_t rt_usbd_function_set_iad(ufunction_t func, uiad_desc_t iad_desc);
|
||||
#endif
|
||||
|
||||
rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep);
|
||||
rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep);
|
||||
rt_err_t rt_usbd_ep0_set_stall(udevice_t device);
|
||||
rt_err_t rt_usbd_ep0_clear_stall(udevice_t device);
|
||||
rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup);
|
||||
rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size);
|
||||
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size);
|
||||
rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size);
|
||||
rt_err_t rt_usbd_reset_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_connect_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_disconnect_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_sof_handler(udcd_t dcd);
|
||||
rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);
|
||||
|
||||
rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->set_address != RT_NULL);
|
||||
|
||||
return dcd->ops->set_address(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_set_config(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->set_config != RT_NULL);
|
||||
|
||||
return dcd->ops->set_config(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_enable(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_enable != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_enable(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_disable(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_disable != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_disable(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_read_prepare(udcd_t dcd, rt_uint8_t address, void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
|
||||
if(dcd->ops->ep_read_prepare != RT_NULL)
|
||||
{
|
||||
return dcd->ops->ep_read_prepare(address, buffer, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_read(udcd_t dcd, rt_uint8_t address, void *buffer)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
|
||||
if(dcd->ops->ep_read != RT_NULL)
|
||||
{
|
||||
return dcd->ops->ep_read(address, buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_write(udcd_t dcd, rt_uint8_t address, void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_write != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_write(address, buffer, size);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep0_send_status(udcd_t dcd)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep0_send_status != RT_NULL);
|
||||
|
||||
return dcd->ops->ep0_send_status();
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_set_stall(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_set_stall != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_set_stall(address);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_clear_stall(udcd_t dcd, rt_uint8_t address)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
RT_ASSERT(dcd->ops != RT_NULL);
|
||||
RT_ASSERT(dcd->ops->ep_clear_stall != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_clear_stall(address);
|
||||
}
|
||||
static rt_inline void usbd_os_proerty_descriptor_send(ufunction_t func, ureq_t setup, usb_os_proerty_t usb_os_proerty, rt_uint8_t number_of_proerty)
|
||||
{
|
||||
struct usb_os_property_header header;
|
||||
static rt_uint8_t * data;
|
||||
rt_uint8_t * pdata;
|
||||
rt_uint8_t index,i;
|
||||
if(data == RT_NULL)
|
||||
{
|
||||
header.dwLength = sizeof(struct usb_os_property_header);
|
||||
header.bcdVersion = 0x0100;
|
||||
header.wIndex = 0x05;
|
||||
header.wCount = number_of_proerty;
|
||||
for(index = 0;index < number_of_proerty;index++)
|
||||
{
|
||||
header.dwLength += usb_os_proerty[index].dwSize;
|
||||
}
|
||||
data = (rt_uint8_t *)rt_malloc(header.dwLength);
|
||||
RT_ASSERT(data != RT_NULL);
|
||||
pdata = data;
|
||||
rt_memcpy((void *)pdata,(void *)&header,sizeof(struct usb_os_property_header));
|
||||
pdata += sizeof(struct usb_os_property_header);
|
||||
for(index = 0;index < number_of_proerty;index++)
|
||||
{
|
||||
rt_memcpy((void *)pdata,(void *)&usb_os_proerty[index],10);
|
||||
pdata += 10;
|
||||
for(i = 0;i < usb_os_proerty[index].wPropertyNameLength/2;i++)
|
||||
{
|
||||
*pdata = usb_os_proerty[index].bPropertyName[i];
|
||||
pdata++;
|
||||
*pdata = 0;
|
||||
pdata++;
|
||||
}
|
||||
*((rt_uint32_t *)pdata) = usb_os_proerty[index].dwPropertyDataLength;
|
||||
pdata += 4;
|
||||
for(i = 0;i < usb_os_proerty[index].dwPropertyDataLength/2;i++)
|
||||
{
|
||||
*pdata = usb_os_proerty[index].bPropertyData[i];
|
||||
pdata++;
|
||||
*pdata = 0;
|
||||
pdata++;
|
||||
}
|
||||
}
|
||||
}
|
||||
rt_usbd_ep0_write(func->device, data, setup->wLength);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
312
sdk/lib/bus/rttusb/include/rttusb_host.h
Normal file
312
sdk/lib/bus/rttusb/include/rttusb_host.h
Normal file
@@ -0,0 +1,312 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-3-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
|
||||
#ifndef __RT_USB_HOST_H__
|
||||
#define __RT_USB_HOST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "include/usb_common.h"
|
||||
|
||||
#define USB_MAX_DEVICE 0x05
|
||||
#define USB_MAX_INTERFACE 0x08
|
||||
#define USB_HUB_PORT_NUM 0x04
|
||||
#define SIZEOF_USB_REQUEST 0x08
|
||||
|
||||
#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
|
||||
|
||||
#define USBH_PID_SETUP 0x00
|
||||
#define USBH_PID_DATA 0x01
|
||||
|
||||
struct uhcd;
|
||||
struct uhintf;
|
||||
struct uhub;
|
||||
struct upipe;
|
||||
|
||||
struct uclass_driver
|
||||
{
|
||||
rt_list_t list;
|
||||
int class_code;
|
||||
int subclass_code;
|
||||
int vendor_id;
|
||||
|
||||
rt_err_t (*enable)(void* arg);
|
||||
rt_err_t (*disable)(void* arg);
|
||||
|
||||
void* user_data;
|
||||
};
|
||||
typedef struct uclass_driver* ucd_t;
|
||||
|
||||
struct uprotocal
|
||||
{
|
||||
rt_list_t list;
|
||||
int pro_id;
|
||||
|
||||
rt_err_t (*init)(void* arg);
|
||||
rt_err_t (*callback)(void* arg);
|
||||
};
|
||||
typedef struct uprotocal* uprotocal_t;
|
||||
|
||||
struct uinstance
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
struct udevice_descriptor dev_desc;
|
||||
ucfg_desc_t cfg_desc;
|
||||
struct uhcd *hcd;
|
||||
|
||||
struct upipe * pipe_ep0_out;
|
||||
struct upipe * pipe_ep0_in;
|
||||
rt_list_t pipe;
|
||||
|
||||
rt_uint8_t status;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t index;
|
||||
rt_uint8_t address;
|
||||
rt_uint8_t speed;
|
||||
rt_uint8_t max_packet_size;
|
||||
rt_uint8_t port;
|
||||
|
||||
struct uhub* parent_hub;
|
||||
struct uhintf* intf[USB_MAX_INTERFACE];
|
||||
};
|
||||
typedef struct uinstance* uinst_t;
|
||||
|
||||
struct uhintf
|
||||
{
|
||||
struct uinstance* device;
|
||||
uintf_desc_t intf_desc;
|
||||
|
||||
ucd_t drv;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
struct upipe
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t pipe_index;
|
||||
rt_uint32_t status;
|
||||
struct uendpoint_descriptor ep;
|
||||
uinst_t inst;
|
||||
func_callback callback;
|
||||
void* user_data;
|
||||
};
|
||||
typedef struct upipe* upipe_t;
|
||||
|
||||
struct uhub
|
||||
{
|
||||
struct uhub_descriptor hub_desc;
|
||||
rt_uint8_t num_ports;
|
||||
rt_uint32_t port_status[USB_HUB_PORT_NUM];
|
||||
struct uinstance* child[USB_HUB_PORT_NUM];
|
||||
|
||||
rt_bool_t is_roothub;
|
||||
|
||||
rt_uint8_t buffer[8];
|
||||
struct uinstance* self;
|
||||
struct uhcd *hcd;
|
||||
};
|
||||
typedef struct uhub* uhub_t;
|
||||
|
||||
struct uhub_param
|
||||
{
|
||||
rt_uint32_t hub_addr;
|
||||
rt_uint32_t hub_mtt_en;
|
||||
rt_uint32_t dev_addr;
|
||||
rt_uint32_t dev_port;
|
||||
rt_uint32_t dev_speed;
|
||||
};
|
||||
typedef struct uhub_param* uhub_param_t;
|
||||
|
||||
struct uhcd_ops
|
||||
{
|
||||
rt_err_t (*reset_port) (rt_uint8_t port);
|
||||
int (*pipe_xfer) (upipe_t pipe, rt_uint8_t token, void* buffer, int nbytes, int timeout);
|
||||
rt_err_t (*open_pipe) (upipe_t pipe);
|
||||
rt_err_t (*close_pipe) (upipe_t pipe);
|
||||
rt_err_t (*hub_port_set) (uhub_param_t param);
|
||||
};
|
||||
typedef struct uhcd_ops* uhcd_ops_t;
|
||||
struct uhcd
|
||||
{
|
||||
struct rt_device parent;
|
||||
uhcd_ops_t ops;
|
||||
rt_uint8_t num_ports;
|
||||
uhub_t roothub;
|
||||
struct rt_messagequeue *usb_mq;
|
||||
rt_thread_t thread;
|
||||
rt_uint32_t thread_status;
|
||||
rt_uint32_t devid;
|
||||
};
|
||||
typedef struct uhcd* uhcd_t;
|
||||
|
||||
enum uhost_msg_type
|
||||
{
|
||||
USB_MSG_CONNECT_CHANGE,
|
||||
USB_MSG_CALLBACK,
|
||||
USB_MSG_CONNECT_IRQ,
|
||||
USB_MSG_DISCONNECT_IRQ,
|
||||
USB_MSG_DELETE_THREAD,
|
||||
};
|
||||
typedef enum uhost_msg_type uhost_msg_type;
|
||||
|
||||
struct uhost_msg
|
||||
{
|
||||
uhost_msg_type type;
|
||||
union
|
||||
{
|
||||
struct uhub* hub;
|
||||
struct
|
||||
{
|
||||
func_callback function;
|
||||
void *context;
|
||||
}cb;
|
||||
}content;
|
||||
};
|
||||
typedef struct uhost_msg* uhost_msg_t;
|
||||
|
||||
rt_err_t hg_usbh_register(rt_uint32_t devid);
|
||||
rt_err_t hg_usbh_unregister(rt_uint32_t devid);
|
||||
int hg_usbd_recover(rt_uint32_t devid);
|
||||
|
||||
/* usb host system interface */
|
||||
rt_err_t rt_usb_host_init(uint32 devid, const char *dev);
|
||||
rt_err_t rt_usb_host_deinit(uint32 devid);
|
||||
void rt_usbh_hub_init(uhcd_t hcd, const char* dev);
|
||||
void rt_usbh_hub_deinit(struct uhcd *hcd);
|
||||
|
||||
/* usb host core interface */
|
||||
struct uinstance* rt_usbh_alloc_instance(uhcd_t uhcd);
|
||||
rt_err_t rt_usbh_hub_alloc_ep0_pipe(uhub_t hub);
|
||||
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_get_string_descriptor(uinst_t device, int intf, 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_interface_associtaion_descriptor(ucfg_desc_t cfg_desc, int num, uiad_desc_t* iad_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_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, int vendor_id);
|
||||
|
||||
/* usb class driver implement */
|
||||
ucd_t rt_usbh_class_driver_hub(void);
|
||||
ucd_t rt_usbh_class_driver_storage(void);
|
||||
|
||||
|
||||
|
||||
/* usb hub interface */
|
||||
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_uint32_t* buffer);
|
||||
rt_err_t rt_usbh_hub_get_port_status(uhub_t uhub, rt_uint16_t port,
|
||||
rt_uint32_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(uhcd_t uhcd, struct uhost_msg* msg);
|
||||
|
||||
|
||||
|
||||
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);
|
||||
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port);
|
||||
|
||||
void analysis_usb_dev_desc(udev_desc_t desc, uint32 desclen);
|
||||
void analysis_usb_cfg_desc(ucfg_desc_t desc, uint32 desclen);
|
||||
void analysis_usb_intf_desc(uintf_desc_t desc, uint32 desclen);
|
||||
void analysis_usb_ep_desc(uep_desc_t ep_desc);
|
||||
void analysis_usb_iad_desc(uiad_desc_t iad_desc);
|
||||
|
||||
/* usb host controller driver interface */
|
||||
rt_inline rt_err_t rt_usb_instance_add_pipe(uinst_t inst, upipe_t pipe)
|
||||
{
|
||||
RT_ASSERT(inst != RT_NULL);
|
||||
RT_ASSERT(pipe != RT_NULL);
|
||||
rt_list_insert_before(&inst->pipe, &pipe->list);
|
||||
return RT_EOK;
|
||||
}
|
||||
rt_inline upipe_t rt_usb_instance_find_pipe(uinst_t inst,rt_uint8_t ep_address)
|
||||
{
|
||||
rt_list_t * l;
|
||||
for(l = inst->pipe.next;l != &inst->pipe;l = l->next)
|
||||
{
|
||||
if(rt_list_entry(l,struct upipe,list)->ep.bEndpointAddress == ep_address)
|
||||
{
|
||||
return rt_list_entry(l,struct upipe,list);
|
||||
}
|
||||
}
|
||||
return RT_NULL;
|
||||
}
|
||||
rt_inline rt_err_t rt_usb_hub_ep0_open_pipe(uhcd_t hcd, uep_desc_t ep)
|
||||
{
|
||||
struct upipe pipe;
|
||||
rt_memset(&pipe,0,sizeof(struct upipe));
|
||||
rt_memcpy(&pipe.ep,ep,sizeof(struct uendpoint_descriptor));
|
||||
return hcd->ops->open_pipe(&pipe);
|
||||
}
|
||||
rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe, uinst_t inst, uep_desc_t ep)
|
||||
{
|
||||
*pipe = (upipe_t)rt_malloc(sizeof(struct upipe));
|
||||
if(*pipe == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_memset(*pipe,0,sizeof(struct upipe));
|
||||
(*pipe)->inst = inst;
|
||||
rt_memcpy(&(*pipe)->ep,ep,sizeof(struct uendpoint_descriptor));
|
||||
return hcd->ops->open_pipe(*pipe);
|
||||
}
|
||||
rt_inline void rt_usb_pipe_add_callback(upipe_t pipe, func_callback callback)
|
||||
{
|
||||
pipe->callback = callback;
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
|
||||
{
|
||||
RT_ASSERT(pipe != RT_NULL);
|
||||
hcd->ops->close_pipe(pipe);
|
||||
rt_free(pipe);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout);
|
||||
rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int timeout)
|
||||
{
|
||||
return hcd->ops->pipe_xfer(pipe, USBH_PID_SETUP, (void *)setup, 8, timeout);
|
||||
}
|
||||
|
||||
int rt_usb_hcd_hub_port_set(uhcd_t hcd, uhub_param_t param);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
627
sdk/lib/bus/rttusb/include/usb_common.h
Normal file
627
sdk/lib/bus/rttusb/include/usb_common.h
Normal file
@@ -0,0 +1,627 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
* 2013-04-26 aozima add DEVICEQUALIFIER support.
|
||||
* 2017-11-15 ZYH fix ep0 transform error
|
||||
*/
|
||||
|
||||
#ifndef __USB_COMMON_H__
|
||||
#define __USB_COMMON_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "hal/usb_ch9.h"
|
||||
|
||||
|
||||
#define RT_DEBUG_USB 0x00
|
||||
#define USB_DYNAMIC 0x00
|
||||
|
||||
/*
|
||||
针对 USB DMA RX , 需做的内存预留大小为 4 字节, 防止 DMA 内存越界引起的内存错误问题
|
||||
|
||||
USB1.1 SIE:
|
||||
(1) rx len % 4 == 1 实际 dma sram 会少 1 byte , 即 rx len - 1 (USB1.1驱动已修复)
|
||||
(2) rx len % 4 == 2 实际 dma sram 会多 1 byte , 即 rx len + 1
|
||||
(3) rx len % 4 == 0 || rx len % 4 == 3 实际 dma sram 长度与 rx len相同 , 即 rx len
|
||||
|
||||
USB2.0 SIE:
|
||||
(1) rx len % 4 == 1 实际 dma sram 会多 2 byte , 即 rx len + 2
|
||||
(2) rx len % 4 == 2 实际 dma sram 会多 1 byte , 即 rx len + 1
|
||||
(3) rx len % 4 == 0 || rx len % 4 == 3 实际 dma sram 长度与 rx len相同 , 即 rx len
|
||||
|
||||
*/
|
||||
#define USB_RX_BUFF_RESERVE_SIZE (4) //接收缓存预留的大小(防止越界)
|
||||
|
||||
#define USB_CLASS_DEVICE 0x00
|
||||
#ifndef _UAPI__LINUX_USB_CH9_H
|
||||
//#define USB_CLASS_AUDIO 0x01
|
||||
//#define USB_CLASS_HID 0x03
|
||||
//#define USB_CLASS_PHYSICAL 0x05
|
||||
#define USB_CLASS_IMAGE 0x06
|
||||
//#define USB_CLASS_PRINTER 0x07
|
||||
//#define USB_CLASS_MASS_STORAGE 0x08
|
||||
#define USB_CLASS_HUB 0x09
|
||||
#define USB_CLASS_CDC_DATA 0x0a
|
||||
#define USB_CLASS_VIDEO 0x0e
|
||||
#define USB_CLASS_MISC 0xef
|
||||
#endif
|
||||
|
||||
#define USB_CLASS_CDC 0x02
|
||||
#define USB_CLASS_APP_SPECIFIC 0xfe
|
||||
#define USB_CLASS_WIRELESS 0xe0
|
||||
#define USB_CLASS_DIAG_DEVICE 0xdc
|
||||
#define USB_CLASS_HEALTHCARE 0x0f
|
||||
#define USB_CLASS_SECURITY 0x0d
|
||||
#define USB_CLASS_SMART_CARD 0x0b
|
||||
#define USB_CLASS_VEND_SPECIFIC 0xff
|
||||
#define USB_DESC_TYPE_DEVICE 0x01
|
||||
#define USB_DESC_TYPE_CONFIGURATION 0x02
|
||||
#define USB_DESC_TYPE_STRING 0x03
|
||||
#define USB_DESC_TYPE_INTERFACE 0x04
|
||||
#define USB_DESC_TYPE_ENDPOINT 0x05
|
||||
#define USB_DESC_TYPE_DEVICEQUALIFIER 0x06
|
||||
#define USB_DESC_TYPE_OTHERSPEED 0x07
|
||||
#define USB_DESC_TYPE_IAD 0x0b
|
||||
#define USB_DESC_TYPE_HID 0x21
|
||||
#define USB_DESC_TYPE_REPORT 0x22
|
||||
#define USB_DESC_TYPE_PHYSICAL 0x23
|
||||
#define USB_DESC_TYPE_HUB 0x29
|
||||
|
||||
#define USB_DESC_LENGTH_DEVICE 0x12
|
||||
#define USB_DESC_LENGTH_CONFIG 0x9
|
||||
#define USB_DESC_LENGTH_IAD 0x8
|
||||
#define USB_DESC_LENGTH_STRING 0x4
|
||||
#define USB_DESC_LENGTH_INTERFACE 0x9
|
||||
#define USB_DESC_LENGTH_ENDPOINT 0x7
|
||||
|
||||
#define USB_REQ_TYPE_STANDARD 0x00
|
||||
#define USB_REQ_TYPE_CLASS 0x20
|
||||
#define USB_REQ_TYPE_VENDOR 0x40
|
||||
#define USB_REQ_TYPE_MASK 0x60
|
||||
|
||||
#define USB_REQ_TYPE_DIR_OUT 0x00
|
||||
#define USB_REQ_TYPE_DIR_IN 0x80
|
||||
|
||||
#define USB_REQ_TYPE_DEVICE 0x00
|
||||
#define USB_REQ_TYPE_INTERFACE 0x01
|
||||
#define USB_REQ_TYPE_ENDPOINT 0x02
|
||||
#define USB_REQ_TYPE_OTHER 0x03
|
||||
#define USB_REQ_TYPE_RECIPIENT_MASK 0x1f
|
||||
|
||||
#define USB_FEATURE_ENDPOINT_HALT 0x00
|
||||
#define USB_FEATURE_DEV_REMOTE_WAKEUP 0x01
|
||||
#define USB_FEATURE_TEST_MODE 0x02
|
||||
|
||||
#define USB_REQ_GET_STATUS 0x00
|
||||
#define USB_REQ_CLEAR_FEATURE 0x01
|
||||
#define USB_REQ_SET_FEATURE 0x03
|
||||
#define USB_REQ_SET_ADDRESS 0x05
|
||||
#define USB_REQ_GET_DESCRIPTOR 0x06
|
||||
#define USB_REQ_SET_DESCRIPTOR 0x07
|
||||
#define USB_REQ_GET_CONFIGURATION 0x08
|
||||
#define USB_REQ_SET_CONFIGURATION 0x09
|
||||
#define USB_REQ_GET_INTERFACE 0x0A
|
||||
#define USB_REQ_SET_INTERFACE 0x0B
|
||||
#define USB_REQ_SYNCH_FRAME 0x0C
|
||||
#define USB_REQ_SET_ENCRYPTION 0x0D
|
||||
#define USB_REQ_GET_ENCRYPTION 0x0E
|
||||
#define USB_REQ_RPIPE_ABORT 0x0E
|
||||
#define USB_REQ_SET_HANDSHAKE 0x0F
|
||||
#define USB_REQ_RPIPE_RESET 0x0F
|
||||
#define USB_REQ_GET_HANDSHAKE 0x10
|
||||
#define USB_REQ_SET_CONNECTION 0x11
|
||||
#define USB_REQ_SET_SECURITY_DATA 0x12
|
||||
#define USB_REQ_GET_SECURITY_DATA 0x13
|
||||
#define USB_REQ_SET_WUSB_DATA 0x14
|
||||
#define USB_REQ_LOOPBACK_DATA_WRITE 0x15
|
||||
#define USB_REQ_LOOPBACK_DATA_READ 0x16
|
||||
#define USB_REQ_SET_INTERFACE_DS 0x17
|
||||
|
||||
#define USB_STRING_LANGID_INDEX 0x00
|
||||
#define USB_STRING_MANU_INDEX 0x01
|
||||
#define USB_STRING_PRODUCT_INDEX 0x02
|
||||
#define USB_STRING_SERIAL_INDEX 0x03
|
||||
#define USB_STRING_CONFIG_INDEX 0x04
|
||||
#define USB_STRING_INTERFACE_INDEX 0x05
|
||||
#define USB_STRING_OS_INDEX 0x06
|
||||
#define USB_STRING_MAX 0xff
|
||||
|
||||
#define USB_STRING_OS "MSFT100A"
|
||||
|
||||
#define USB_PID_OUT 0x01
|
||||
#define USB_PID_ACK 0x02
|
||||
#define USB_PID_DATA0 0x03
|
||||
#define USB_PID_SOF 0x05
|
||||
#define USB_PID_IN 0x09
|
||||
#define USB_PID_NACK 0x0A
|
||||
#define USB_PID_DATA1 0x0B
|
||||
#define USB_PID_PRE 0x0C
|
||||
#define USB_PID_SETUP 0x0D
|
||||
#define USB_PID_STALL 0x0E
|
||||
|
||||
#define USB_EP_DESC_OUT 0x00
|
||||
#define USB_EP_DESC_IN 0x80
|
||||
#define USB_EP_DESC_NUM_MASK 0x0f
|
||||
|
||||
#define USB_EP_ATTR_CONTROL 0x00
|
||||
#define USB_EP_ATTR_ISOC 0x01
|
||||
#define USB_EP_ATTR_BULK 0x02
|
||||
#define USB_EP_ATTR_INT 0x03
|
||||
#define USB_EP_ATTR_TYPE_MASK 0x03
|
||||
|
||||
#define USB_EPNO_MASK 0x7f
|
||||
#ifndef _UAPI__LINUX_USB_CH9_H
|
||||
#define USB_DIR_OUT 0x00
|
||||
#define USB_DIR_IN 0x80
|
||||
#endif
|
||||
#define USB_DIR_INOUT 0x40
|
||||
#define USB_DIR_MASK 0x80
|
||||
|
||||
/* wMaxPacketSize in Endpoint Descriptor */
|
||||
#define USB_MAXPACKETSIZE_SHIFT 0
|
||||
#define USB_MAXPACKETSIZE_MASK (0x7ff << USB_MAXPACKETSIZE_SHIFT)
|
||||
#define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT 11
|
||||
#define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_NONE (0 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
|
||||
#define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_ONE (1 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
|
||||
#define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_TWO (2 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
|
||||
#define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK (3 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
|
||||
#define USB_GET_MAXPACKETSIZE(x) ((x & USB_MAXPACKETSIZE_MASK) >> USB_MAXPACKETSIZE_SHIFT)
|
||||
#define USB_GET_MULT(x) ((x & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
|
||||
|
||||
#define ID_UNASSIGNED 0
|
||||
#define ID_ASSIGNED 1
|
||||
|
||||
#define RH_GET_PORT_STATUS 0
|
||||
#define RH_SET_PORT_STATUS 1
|
||||
#define RH_CLEAR_PORT_FEATURE 2
|
||||
#define RH_SET_PORT_FEATURE 3
|
||||
|
||||
#define USB_BUS_POWERED 0
|
||||
#define USB_SELF_POWERED 1
|
||||
#define USB_REMOTE_WAKEUP 1
|
||||
#define USB_EP_HALT 0
|
||||
|
||||
/*
|
||||
* Port feature numbers
|
||||
*/
|
||||
#define PORT_FEAT_CONNECTION 0
|
||||
#define PORT_FEAT_ENABLE 1
|
||||
#define PORT_FEAT_SUSPEND 2
|
||||
#define PORT_FEAT_OVER_CURRENT 3
|
||||
#define PORT_FEAT_RESET 4
|
||||
#define PORT_FEAT_POWER 8
|
||||
#define PORT_FEAT_LOWSPEED 9
|
||||
#define PORT_FEAT_HIGHSPEED 10
|
||||
#define PORT_FEAT_C_CONNECTION 16
|
||||
#define PORT_FEAT_C_ENABLE 17
|
||||
#define PORT_FEAT_C_SUSPEND 18
|
||||
#define PORT_FEAT_C_OVER_CURRENT 19
|
||||
#define PORT_FEAT_C_RESET 20
|
||||
|
||||
/*
|
||||
The HcRhPortStatus[1:NDP] register is used to control and report port events on a per-port
|
||||
basis. NumberDownstreamPorts represents the number of HcRhPortStatus registers that are
|
||||
implemented in hardware. The lower word is used to reflect the port status, whereas the upper
|
||||
word reflects the status change bits. Some status bits are implemented with special write behavior
|
||||
(see below). If a transaction (token through handshake) is in progress when a write to change
|
||||
port status occurs, the resulting port status change must be postponed until the transaction
|
||||
completes. Reserved bits should always be written '0'.
|
||||
*/
|
||||
#define PORT_CCS 0x00000001UL /* R:CurrentConnectStatus - W:ClearPortEnable */
|
||||
#define PORT_PES 0x00000002UL /* R:PortEnableStatus - W:SetPortEnable */
|
||||
#define PORT_PSS 0x00000004UL /* R:PortSuspendStatus - W:SetPortSuspend */
|
||||
#define PORT_POCI 0x00000008UL /* R:PortOverCurrentIndicator - W:ClearSuspendStatus */
|
||||
#define PORT_PRS 0x00000010UL /* R:PortResetStatus - W: SetPortReset */
|
||||
#define PORT_PPS 0x00000100UL /* R:PortPowerStatus - W: SetPortPower */
|
||||
#define PORT_LSDA 0x00000200UL /* R:LowSpeedDeviceAttached - W:ClearPortPower */
|
||||
#define PORT_HSDA 0x00000400UL /* R:HighSpeedDeviceAttached - W:ClearPortPower */
|
||||
#define PORT_CCSC 0x00010000UL
|
||||
#define PORT_PESC 0x00020000UL
|
||||
#define PORT_PSSC 0x00040000UL
|
||||
#define PORT_POCIC 0x00080000UL
|
||||
#define PORT_PRSC 0x00100000UL
|
||||
|
||||
/*
|
||||
*Hub Status & Hub Change bit masks
|
||||
*/
|
||||
#define HUB_STATUS_LOCAL_POWER 0x0001
|
||||
#define HUB_STATUS_OVERCURRENT 0x0002
|
||||
|
||||
#define HUB_CHANGE_LOCAL_POWER 0x0001
|
||||
#define HUB_CHANGE_OVERCURRENT 0x0002
|
||||
|
||||
#define USB_EP_ATTR(attr) (attr & USB_EP_ATTR_TYPE_MASK)
|
||||
#define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
|
||||
#define USB_EP_DIR(addr) ((addr & USB_DIR_MASK)>>7)
|
||||
|
||||
#define HID_REPORT_ID_KEYBOARD1 1
|
||||
#define HID_REPORT_ID_KEYBOARD2 2
|
||||
#define HID_REPORT_ID_KEYBOARD3 3
|
||||
#define HID_REPORT_ID_KEYBOARD4 7
|
||||
#define HID_REPORT_ID_MEDIA 4
|
||||
#define HID_REPORT_ID_GENERAL 5
|
||||
#define HID_REPORT_ID_MOUSE 6
|
||||
|
||||
/*
|
||||
* Time of usb timeout
|
||||
*/
|
||||
#ifndef USB_TIMEOUT_BASIC
|
||||
#define USB_TIMEOUT_BASIC (RT_TICK_PER_SECOND) /* 1s */
|
||||
#endif
|
||||
#ifndef USB_TIMEOUT_LONG
|
||||
#define USB_TIMEOUT_LONG (RT_TICK_PER_SECOND * 5) /* 5s */
|
||||
#endif
|
||||
#ifndef USB_DEBOUNCE_TIME
|
||||
#define USB_DEBOUNCE_TIME (RT_TICK_PER_SECOND / 5) /* 0.2s */
|
||||
#endif
|
||||
|
||||
#define uswap_32(x) \
|
||||
((((x) & 0xff000000) >> 24) | \
|
||||
(((x) & 0x00ff0000) >> 8) | \
|
||||
(((x) & 0x0000ff00) << 8) | \
|
||||
(((x) & 0x000000ff) << 24))
|
||||
|
||||
#define uswap_8(x) \
|
||||
(((rt_uint16_t)(*((rt_uint8_t *)(x)))) + \
|
||||
(((rt_uint16_t)(*(((rt_uint8_t *)(x)) + 1))) << 8))
|
||||
|
||||
typedef void (*func_callback)(void *context);
|
||||
//typedef enum
|
||||
//{
|
||||
// USB_STATE_NOTATTACHED = 0,
|
||||
// USB_STATE_ATTACHED,
|
||||
// USB_STATE_POWERED,
|
||||
// USB_STATE_RECONNECTING,
|
||||
// USB_STATE_UNAUTHENTICATED,
|
||||
// USB_STATE_DEFAULT,
|
||||
// USB_STATE_ADDRESS,
|
||||
// USB_STATE_CONFIGURED,
|
||||
// USB_STATE_SUSPENDED
|
||||
//}udevice_state_t;
|
||||
typedef enum usb_device_state udevice_state_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STAGE_IDLE,
|
||||
STAGE_SETUP,
|
||||
STAGE_STATUS_IN,
|
||||
STAGE_STATUS_OUT,
|
||||
STAGE_DIN,
|
||||
STAGE_DOUT
|
||||
} uep0_stage_t;
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
struct usb_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
};
|
||||
typedef struct usb_descriptor* udesc_t;
|
||||
|
||||
struct udevice_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t bcdUSB;
|
||||
rt_uint8_t bDeviceClass;
|
||||
rt_uint8_t bDeviceSubClass;
|
||||
rt_uint8_t bDeviceProtocol;
|
||||
rt_uint8_t bMaxPacketSize0;
|
||||
rt_uint16_t idVendor;
|
||||
rt_uint16_t idProduct;
|
||||
rt_uint16_t bcdDevice;
|
||||
rt_uint8_t iManufacturer;
|
||||
rt_uint8_t iProduct;
|
||||
rt_uint8_t iSerialNumber;
|
||||
rt_uint8_t bNumConfigurations;
|
||||
};
|
||||
typedef struct udevice_descriptor* udev_desc_t;
|
||||
|
||||
struct uconfig_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t wTotalLength;
|
||||
rt_uint8_t bNumInterfaces;
|
||||
rt_uint8_t bConfigurationValue;
|
||||
rt_uint8_t iConfiguration;
|
||||
rt_uint8_t bmAttributes;
|
||||
rt_uint8_t MaxPower;
|
||||
rt_uint8_t data[2048];
|
||||
};
|
||||
typedef struct uconfig_descriptor* ucfg_desc_t;
|
||||
|
||||
struct uinterface_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t bInterfaceNumber;
|
||||
rt_uint8_t bAlternateSetting;
|
||||
rt_uint8_t bNumEndpoints;
|
||||
rt_uint8_t bInterfaceClass;
|
||||
rt_uint8_t bInterfaceSubClass;
|
||||
rt_uint8_t bInterfaceProtocol;
|
||||
rt_uint8_t iInterface;
|
||||
};
|
||||
typedef struct uinterface_descriptor* uintf_desc_t;
|
||||
|
||||
/* Interface Association Descriptor (IAD) */
|
||||
struct uiad_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t bDescriptorType;
|
||||
rt_uint8_t bFirstInterface;
|
||||
rt_uint8_t bInterfaceCount;
|
||||
rt_uint8_t bFunctionClass;
|
||||
rt_uint8_t bFunctionSubClass;
|
||||
rt_uint8_t bFunctionProtocol;
|
||||
rt_uint8_t iFunction;
|
||||
};
|
||||
typedef struct uiad_descriptor* uiad_desc_t;
|
||||
|
||||
struct uendpoint_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t bEndpointAddress;
|
||||
rt_uint8_t bmAttributes;
|
||||
rt_uint16_t wMaxPacketSize;
|
||||
rt_uint8_t bInterval;
|
||||
};
|
||||
typedef struct uendpoint_descriptor* uep_desc_t;
|
||||
|
||||
struct ustring_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t String[64];
|
||||
};
|
||||
typedef struct ustring_descriptor* ustr_desc_t;
|
||||
|
||||
struct uhub_descriptor
|
||||
{
|
||||
rt_uint8_t length;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t num_ports;
|
||||
rt_uint16_t characteristics;
|
||||
rt_uint8_t pwron_to_good; /* power on to power good */
|
||||
rt_uint8_t current;
|
||||
rt_uint8_t removable[8];
|
||||
rt_uint8_t pwr_ctl[8];
|
||||
};
|
||||
typedef struct uhub_descriptor* uhub_desc_t;
|
||||
|
||||
/* USB_DESC_TYPE_DEVICEQUALIFIER: Device Qualifier descriptor */
|
||||
//struct usb_qualifier_descriptor
|
||||
//{
|
||||
// rt_uint8_t bLength;
|
||||
// rt_uint8_t bDescriptorType;
|
||||
//
|
||||
// rt_uint16_t bcdUSB; // TODO: big-endian.
|
||||
// rt_uint8_t bDeviceClass;
|
||||
// rt_uint8_t bDeviceSubClass;
|
||||
// rt_uint8_t bDeviceProtocol;
|
||||
// rt_uint8_t bMaxPacketSize0;
|
||||
// rt_uint8_t bNumConfigurations;
|
||||
// rt_uint8_t bRESERVED;
|
||||
//} __attribute__ ((packed));
|
||||
|
||||
struct usb_os_header_comp_id_descriptor
|
||||
{
|
||||
rt_uint32_t dwLength;
|
||||
rt_uint16_t bcdVersion;
|
||||
rt_uint16_t wIndex;
|
||||
rt_uint8_t bCount;
|
||||
rt_uint8_t reserved[7];
|
||||
};
|
||||
typedef struct usb_os_header_comp_id_descriptor * usb_os_header_desc_t;
|
||||
|
||||
struct usb_os_property_header
|
||||
{
|
||||
rt_uint32_t dwLength;
|
||||
rt_uint16_t bcdVersion;
|
||||
rt_uint16_t wIndex;
|
||||
rt_uint16_t wCount;
|
||||
};
|
||||
typedef struct usb_os_property_header * usb_os_property_header_t;
|
||||
struct usb_os_proerty
|
||||
{
|
||||
rt_uint32_t dwSize;
|
||||
rt_uint32_t dwPropertyDataType;
|
||||
rt_uint16_t wPropertyNameLength;
|
||||
const char * bPropertyName;
|
||||
rt_uint32_t dwPropertyDataLength;
|
||||
const char * bPropertyData;
|
||||
};
|
||||
typedef struct usb_os_proerty * usb_os_proerty_t;
|
||||
|
||||
// Value Description
|
||||
// 1 A NULL-terminated Unicode String (REG_SZ)
|
||||
// 2 A NULL-terminated Unicode String that includes environment variables (REG_EXPAND_SZ)
|
||||
// 3 Free-form binary (REG_BINARY)
|
||||
// 4 A little-endian 32-bit integer (REG_DWORD_LITTLE_ENDIAN)
|
||||
// 5 A big-endian 32-bit integer (REG_DWORD_BIG_ENDIAN)
|
||||
// 6 A NULL-terminated Unicode string that contains a symbolic link (REG_LINK)
|
||||
// 7 Multiple NULL-terminated Unicode strings (REG_MULTI_SZ)
|
||||
#define USB_OS_PROPERTY_TYPE_REG_SZ 0x01UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_EXPAND_SZ 0x02UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_BINARY 0x03UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_DWORD_LITTLE_ENDIAN 0x04UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_DWORD_BIG_ENDIAN 0x05UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_LINK 0x06UL
|
||||
#define USB_OS_PROPERTY_TYPE_REG_MULTI_SZ 0x07UL
|
||||
|
||||
#define USB_OS_PROPERTY_DESC(PropertyDataType,PropertyName,PropertyData) \
|
||||
{\
|
||||
.dwSize = sizeof(struct usb_os_proerty)-sizeof(const char *)*2\
|
||||
+sizeof(PropertyName)*2+sizeof(PropertyData)*2,\
|
||||
.dwPropertyDataType = PropertyDataType,\
|
||||
.wPropertyNameLength = sizeof(PropertyName)*2,\
|
||||
.bPropertyName = PropertyName,\
|
||||
.dwPropertyDataLength = sizeof(PropertyData)*2,\
|
||||
.bPropertyData = PropertyData\
|
||||
}
|
||||
|
||||
|
||||
#ifndef HID_SUB_DESCRIPTOR_MAX
|
||||
#define HID_SUB_DESCRIPTOR_MAX 1
|
||||
#endif
|
||||
|
||||
struct uhid_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t bcdHID;
|
||||
rt_uint8_t bCountryCode;
|
||||
rt_uint8_t bNumDescriptors;
|
||||
struct hid_descriptor_list
|
||||
{
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t wLength;
|
||||
}Descriptor[HID_SUB_DESCRIPTOR_MAX];
|
||||
};
|
||||
typedef struct uhid_descriptor* uhid_desc_t;
|
||||
|
||||
struct hid_report
|
||||
{
|
||||
rt_uint8_t report_id;
|
||||
rt_uint8_t report[63];
|
||||
rt_uint8_t size;
|
||||
};
|
||||
typedef struct hid_report* hid_report_t;
|
||||
extern void HID_Report_Received(hid_report_t report);
|
||||
|
||||
struct urequest
|
||||
{
|
||||
rt_uint8_t request_type;
|
||||
rt_uint8_t bRequest;
|
||||
rt_uint16_t wValue;
|
||||
rt_uint16_t wIndex;
|
||||
rt_uint16_t wLength;
|
||||
};
|
||||
typedef struct urequest* ureq_t;
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a < b ? a : b)
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) (a > b ? a : b)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* the define related to mass storage
|
||||
*/
|
||||
#define USBREQ_GET_MAX_LUN 0xfe
|
||||
#define USBREQ_MASS_STORAGE_RESET 0xff
|
||||
|
||||
#define SIZEOF_CSW 0x0d
|
||||
#define SIZEOF_CBW 0x1f
|
||||
#define SIZEOF_INQUIRY_CMD 0x24
|
||||
#define SIZEOF_MODE_SENSE_6 0x4
|
||||
#define SIZEOF_MODE_SENSE_10 0x4
|
||||
#define SIZEOF_READ_CAPACITIES 0xc
|
||||
#define SIZEOF_READ_CAPACITY 0x8
|
||||
#define SIZEOF_REQUEST_SENSE 0x12
|
||||
|
||||
#define CBWFLAGS_DIR_M 0x80
|
||||
#define CBWFLAGS_DIR_IN 0x80
|
||||
#define CBWFLAGS_DIR_OUT 0x00
|
||||
|
||||
#define SCSI_TEST_UNIT_READY 0x00
|
||||
#define SCSI_REQUEST_SENSE 0x03
|
||||
#define SCSI_INQUIRY_CMD 0x12
|
||||
#define SCSI_ALLOW_REMOVAL 0x1e
|
||||
#define SCSI_MODE_SENSE_6 0x1a
|
||||
#define SCSI_START_STOP 0x1b
|
||||
#define SCSI_READ_CAPACITIES 0x23
|
||||
#define SCSI_READ_CAPACITY 0x25
|
||||
#define SCSI_READ_10 0x28
|
||||
#define SCSI_WRITE_10 0x2a
|
||||
#define SCSI_VERIFY_10 0x2f
|
||||
#define SCSI_MODE_SENSE_10 0x5a
|
||||
|
||||
#define CBW_SIGNATURE 0x43425355
|
||||
#define CSW_SIGNATURE 0x53425355
|
||||
#define CBW_TAG_VALUE 0x12345678
|
||||
|
||||
struct ustorage_cbw
|
||||
{
|
||||
rt_uint32_t signature;
|
||||
rt_uint32_t tag;
|
||||
rt_uint32_t xfer_len;
|
||||
rt_uint8_t dflags;
|
||||
rt_uint8_t lun;
|
||||
rt_uint8_t cb_len;
|
||||
rt_uint8_t cb[16];
|
||||
};
|
||||
typedef struct ustorage_cbw* ustorage_cbw_t;
|
||||
|
||||
struct ustorage_csw
|
||||
{
|
||||
rt_uint32_t signature;
|
||||
rt_uint32_t tag;
|
||||
rt_int32_t data_reside;
|
||||
rt_uint8_t status;
|
||||
};
|
||||
typedef struct ustorage_csw* ustorage_csw_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
struct usb_os_comp_id_descriptor
|
||||
{
|
||||
struct usb_os_header_comp_id_descriptor head_desc;
|
||||
rt_list_t func_desc;
|
||||
};
|
||||
typedef struct usb_os_comp_id_descriptor * usb_os_comp_id_desc_t;
|
||||
|
||||
struct usb_os_function_comp_id_descriptor
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t bFirstInterfaceNumber;
|
||||
rt_uint8_t reserved1;
|
||||
rt_uint8_t compatibleID[8];
|
||||
rt_uint8_t subCompatibleID[8];
|
||||
rt_uint8_t reserved2[6];
|
||||
};
|
||||
typedef struct usb_os_function_comp_id_descriptor * usb_os_func_comp_id_desc_t;
|
||||
|
||||
/*
|
||||
* USB device event loop thread configurations
|
||||
*/
|
||||
/* the stack size of USB thread */
|
||||
#ifndef RT_USBD_THREAD_STACK_SZ
|
||||
#define RT_USBD_THREAD_STACK_SZ 1024
|
||||
#endif
|
||||
|
||||
/* the priority of USB thread */
|
||||
#ifndef RT_USBD_THREAD_PRIO
|
||||
#define RT_USBD_THREAD_PRIO (OS_TASK_PRIORITY_HIGH-1)
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RTTUSB_DEV_NONE = 0,
|
||||
RTTUSB_DEV_LowSpeed,
|
||||
RTTUSB_DEV_FullSpeed,
|
||||
RTTUSB_DEV_HighSpeed,
|
||||
} usb_dev_speed_t;
|
||||
|
||||
void hg_usb_connect_detect_using(void);
|
||||
void hg_usb_connect_detect_recfg(void);
|
||||
void hg_usb_connect_detect_init(void);
|
||||
void hg_usb_connect_detect_deinit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
1382
sdk/lib/bus/rttusb/include/usb_video.h
Normal file
1382
sdk/lib/bus/rttusb/include/usb_video.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user