2013-01-08 22:40:58 +08:00
|
|
|
/*
|
2018-10-09 11:43:35 +08:00
|
|
|
* File : hid.c
|
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-09 11:43:35 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2013-01-09 18:07:59 +08:00
|
|
|
* 2012-10-02 Yi Qiu first version
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <rtservice.h>
|
|
|
|
|
2013-01-09 18:07:59 +08:00
|
|
|
#ifdef RT_USING_USB_DEVICE
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
#define USB_DEVICE_CONTROLLER_NAME "usbd"
|
|
|
|
|
2013-01-24 20:59:43 +08:00
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
const static char* ustring[] =
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
"Language",
|
|
|
|
"RT-Thread Team.",
|
2013-01-24 20:59:43 +08:00
|
|
|
"RTT Composite Device",
|
2014-02-16 17:42:35 +08:00
|
|
|
"320219198301",
|
2013-01-08 22:40:58 +08:00
|
|
|
"Configuration",
|
|
|
|
"Interface",
|
2017-11-23 00:37:21 +08:00
|
|
|
USB_STRING_OS
|
2013-01-08 22:40:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct udevice_descriptor compsit_desc =
|
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_DEVICE, //bLength;
|
|
|
|
USB_DESC_TYPE_DEVICE, //type;
|
|
|
|
USB_BCD_VERSION, //bcdUSB;
|
|
|
|
USB_CLASS_MISC, //bDeviceClass;
|
|
|
|
0x02, //bDeviceSubClass;
|
|
|
|
0x01, //bDeviceProtocol;
|
|
|
|
0x40, //bMaxPacketSize0;
|
2013-01-09 18:07:59 +08:00
|
|
|
_VENDOR_ID, //idVendor;
|
|
|
|
_PRODUCT_ID, //idProduct;
|
2013-01-08 22:40:58 +08:00
|
|
|
USB_BCD_DEVICE, //bcdDevice;
|
|
|
|
USB_STRING_MANU_INDEX, //iManufacturer;
|
|
|
|
USB_STRING_PRODUCT_INDEX, //iProduct;
|
|
|
|
USB_STRING_SERIAL_INDEX, //iSerialNumber;
|
2013-01-24 20:59:43 +08:00
|
|
|
USB_DYNAMIC, //bNumConfigurations;
|
2013-01-08 22:40:58 +08:00
|
|
|
};
|
2018-03-16 18:40:07 +08:00
|
|
|
|
|
|
|
//FS and HS needed
|
|
|
|
static struct usb_qualifier_descriptor dev_qualifier =
|
|
|
|
{
|
|
|
|
sizeof(dev_qualifier), //bLength
|
|
|
|
USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
|
|
|
|
0x0200, //bcdUSB
|
|
|
|
USB_CLASS_MISC, //bDeviceClass
|
|
|
|
0x02, //bDeviceSubClass
|
|
|
|
0x01, //bDeviceProtocol
|
|
|
|
64, //bMaxPacketSize0
|
|
|
|
0x01, //bNumConfigurations
|
|
|
|
0,
|
|
|
|
};
|
2013-01-08 22:40:58 +08:00
|
|
|
#endif
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
struct usb_os_comp_id_descriptor usb_comp_id_desc =
|
2017-11-23 00:37:21 +08:00
|
|
|
{
|
|
|
|
//head section
|
2018-08-28 03:18:39 +08:00
|
|
|
{
|
|
|
|
USB_DYNAMIC,
|
|
|
|
0x0100,
|
|
|
|
0x04,
|
|
|
|
USB_DYNAMIC,
|
|
|
|
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
|
|
|
},
|
2017-11-23 00:37:21 +08:00
|
|
|
};
|
2018-04-24 12:48:05 +08:00
|
|
|
static rt_list_t class_list;
|
|
|
|
int rt_usbd_class_list_init(void)
|
|
|
|
{
|
|
|
|
rt_list_init(&class_list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(rt_usbd_class_list_init);
|
|
|
|
|
|
|
|
rt_err_t rt_usbd_class_register(udclass_t udclass)
|
|
|
|
{
|
2018-10-09 11:41:38 +08:00
|
|
|
#ifndef RT_USB_DEVICE_COMPOSITE
|
|
|
|
if(!rt_list_isempty(&class_list))
|
|
|
|
{
|
|
|
|
rt_kprintf("[D/USBD] If you want to use usb composite device please define RT_USB_DEVICE_COMPOSITE\n");
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-24 12:48:05 +08:00
|
|
|
rt_list_insert_before(&class_list,&udclass->list);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2017-11-23 00:37:21 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_err_t rt_usb_device_init(void)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_device_t udc;
|
|
|
|
udevice_t udevice;
|
|
|
|
uconfig_t cfg;
|
2014-02-16 17:42:35 +08:00
|
|
|
ufunction_t func;
|
2018-04-24 12:48:05 +08:00
|
|
|
rt_list_t *i;
|
|
|
|
udclass_t udclass;
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2018-10-09 11:41:38 +08:00
|
|
|
if(rt_list_isempty(&class_list))
|
|
|
|
{
|
|
|
|
rt_kprintf("[D/USBD] No class register on usb device\n");
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
/* create and startup usb device thread */
|
|
|
|
rt_usbd_core_init();
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* create a device object */
|
|
|
|
udevice = rt_usbd_device_new();
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
udc = rt_device_find(USB_DEVICE_CONTROLLER_NAME);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(udc == RT_NULL)
|
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_kprintf("can't find usb device controller %s\n", USB_DEVICE_CONTROLLER_NAME);
|
2013-01-08 22:40:58 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set usb controller driver to the device */
|
|
|
|
rt_usbd_device_set_controller(udevice, (udcd_t)udc);
|
|
|
|
|
|
|
|
/* create a configuration object */
|
2014-02-16 17:42:35 +08:00
|
|
|
cfg = rt_usbd_config_new();
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2018-04-24 12:48:05 +08:00
|
|
|
rt_usbd_device_set_os_comp_id_desc(udevice, &usb_comp_id_desc);
|
2017-11-23 00:37:21 +08:00
|
|
|
|
2018-04-24 12:48:05 +08:00
|
|
|
for(i = class_list.next; i!= &class_list; i = i->next)
|
2017-11-11 10:51:47 +08:00
|
|
|
{
|
2018-04-24 12:48:05 +08:00
|
|
|
/* get a class creater */
|
|
|
|
udclass = rt_list_entry(i, struct udclass, list);
|
|
|
|
/* create a function object */
|
|
|
|
func = udclass->rt_usbd_function_create(udevice);
|
2017-11-11 10:51:47 +08:00
|
|
|
/* add the function to the configuration */
|
|
|
|
rt_usbd_config_add_function(cfg, func);
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
/* set device descriptor to the device */
|
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
rt_usbd_device_set_descriptor(udevice, &compsit_desc);
|
2013-01-24 20:59:43 +08:00
|
|
|
rt_usbd_device_set_string(udevice, ustring);
|
2018-04-24 12:48:05 +08:00
|
|
|
if(udevice->dcd->device_is_hs)
|
|
|
|
{
|
|
|
|
rt_usbd_device_set_qualifier(udevice, &dev_qualifier);
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
#else
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_device_set_descriptor(udevice, func->dev_desc);
|
2013-01-08 22:40:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* add the configuration to the device */
|
|
|
|
rt_usbd_device_add_config(udevice, cfg);
|
2013-01-24 20:59:43 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* initialize usb device controller */
|
|
|
|
rt_device_init(udc);
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* set default configuration to 1 */
|
|
|
|
rt_usbd_set_config(udevice, 1);
|
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
2013-01-09 18:07:59 +08:00
|
|
|
#endif
|