RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
usb_device.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2012-10-01 Yi Qiu first version
9 * 2012-12-12 heyuanjie87 change endpoint and function handler
10 * 2013-04-26 aozima add DEVICEQUALIFIER support.
11 * 2017-11-15 ZYH fix ep0 transform error
12 * 2023-10-11 ChuShicheng change rt_size_t to rt_ssize_t
13 */
14
15#ifndef __USB_DEVICE_H__
16#define __USB_DEVICE_H__
17
18#include <rtthread.h>
19#include "drivers/usb_common.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25/* Vendor ID */
26#ifdef USB_VENDOR_ID
27#define _VENDOR_ID USB_VENDOR_ID
28#else
29#define _VENDOR_ID 0x0EFF
30#endif
31/* Product ID */
32#ifdef USB_PRODUCT_ID
33#define _PRODUCT_ID USB_PRODUCT_ID
34#else
35#define _PRODUCT_ID 0x0001
36#endif
37
38#ifndef MAX_INTF_STR
39#define MAX_INTF_STR 20
40#endif
41
42#define USB_BCD_DEVICE 0x0200 /* USB Specification Release Number in Binary-Coded Decimal */
43#define USB_BCD_VERSION 0x0200 /* USB 2.0 */
44#define EP0_IN_ADDR 0x80
45#define EP0_OUT_ADDR 0x00
46#define EP_HANDLER(ep, func, size) RT_ASSERT(ep != RT_NULL); ep->handler(func, size)
47#define EP_ADDRESS(ep) ep->ep_desc->bEndpointAddress
48#define EP_MAXPACKET(ep) ep->ep_desc->wMaxPacketSize
49#define FUNC_ENABLE(func) do{ \
50 if(func->ops->enable != RT_NULL && \
51 func->enabled == RT_FALSE) \
52 { \
53 if(func->ops->enable(func) == RT_EOK) \
54 func->enabled = RT_TRUE; \
55 } \
56 }while(0)
57#define FUNC_DISABLE(func) do{ \
58 if(func->ops->disable != RT_NULL && \
59 func->enabled == RT_TRUE) \
60 { \
61 func->enabled = RT_FALSE; \
62 func->ops->disable(func); \
63 } \
64 }while(0)
65
66#define RT_USBD_CLASS_CTRL_CONNECTED (RT_DEVICE_CTRL_BASE(USBDevice) + 0)
67
68struct ufunction;
69struct udevice;
70struct uendpoint;
71
72typedef enum
73{
74 /* request to read full count */
76 /* request to read any count */
78 /* request to write full count */
81
83{
90 rt_ssize_t (*ep_read_prepare)(rt_uint8_t address, void *buffer, rt_size_t size);
91 rt_ssize_t (*ep_read)(rt_uint8_t address, void *buffer);
92 rt_ssize_t (*ep_write)(rt_uint8_t address, void *buffer, rt_size_t size);
94 rt_err_t (*suspend)(void);
95 rt_err_t (*wakeup)(void);
96};
97
106
107typedef rt_err_t (*udep_handler_t)(struct ufunction* func, rt_size_t size);
108
118
131typedef struct uendpoint* uep_t;
132
142typedef struct udcd* udcd_t;
143
153
154typedef rt_err_t (*uintf_handler_t)(struct ufunction* func, ureq_t setup);
155
164typedef struct uinterface* uintf_t;
165
167{
168 rt_err_t (*enable)(struct ufunction* func);
169 rt_err_t (*disable)(struct ufunction* func);
171};
173
185typedef struct ufunction* ufunction_t;
186
193typedef struct uconfig* uconfig_t;
194
211typedef struct udevice* udevice_t;
212
218typedef struct udclass* udclass_t;
219
221{
229 /* we don't need to add a "PLUG_IN" event because after the cable is
230 * plugged in(before any SETUP) the classed have nothing to do. If the host
231 * is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
232 * should reset and run the class while plug_in is not. */
234};
236
242
244{
247 union
248 {
252};
253typedef struct udev_msg* udev_msg_t;
254
259 ufunction_ops_t ops);
263
269rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
270rt_err_t rt_usbd_device_set_interface_string(udevice_t device, int index, const char* string);
283
289rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size);
291 rt_err_t (*rx_ind)(udevice_t device, rt_size_t size));
292
299
300#ifdef RT_USB_DEVICE_COMPOSITE
301rt_err_t rt_usbd_function_set_iad(ufunction_t func, uiad_desc_t iad_desc);
302#endif
303
319
321{
325
326 return dcd->ops->set_address(address);
327}
328
330{
334
335 return dcd->ops->set_config(address);
336}
337
339{
343
344 return dcd->ops->ep_enable(ep);
345}
346
348{
352
353 return dcd->ops->ep_disable(ep);
354}
355
356rt_inline rt_size_t dcd_ep_read_prepare(udcd_t dcd, rt_uint8_t address, void *buffer,
357 rt_size_t size)
358{
361
363 {
364 return dcd->ops->ep_read_prepare(address, buffer, size);
365 }
366 else
367 {
368 return 0;
369 }
370}
371
372rt_inline rt_size_t dcd_ep_read(udcd_t dcd, rt_uint8_t address, void *buffer)
373{
376
377 if(dcd->ops->ep_read != RT_NULL)
378 {
379 return dcd->ops->ep_read(address, buffer);
380 }
381 else
382 {
383 return 0;
384 }
385}
386
387rt_inline rt_size_t dcd_ep_write(udcd_t dcd, rt_uint8_t address, void *buffer,
388 rt_size_t size)
389{
393
394 return dcd->ops->ep_write(address, buffer, size);
395}
396
405
407{
411
412 return dcd->ops->ep_set_stall(address);
413}
414
416{
420
421 return dcd->ops->ep_clear_stall(address);
422}
424{
425 struct usb_os_property_header header;
426 static rt_uint8_t * data;
427 rt_uint8_t * pdata;
428 rt_uint8_t index,i;
429 if(data == RT_NULL)
430 {
431 header.dwLength = sizeof(struct usb_os_property_header);
432 header.bcdVersion = 0x0100;
433 header.wIndex = 0x05;
434 header.wCount = number_of_proerty;
435 for(index = 0;index < number_of_proerty;index++)
436 {
437 header.dwLength += usb_os_proerty[index].dwSize;
438 }
439 data = (rt_uint8_t *)rt_malloc(header.dwLength);
440 RT_ASSERT(data != RT_NULL);
441 pdata = data;
442 rt_memcpy((void *)pdata,(void *)&header,sizeof(struct usb_os_property_header));
443 pdata += sizeof(struct usb_os_property_header);
444 for(index = 0;index < number_of_proerty;index++)
445 {
446 rt_memcpy((void *)pdata,(void *)&usb_os_proerty[index],10);
447 pdata += 10;
448 for(i = 0;i < usb_os_proerty[index].wPropertyNameLength/2;i++)
449 {
450 *pdata = usb_os_proerty[index].bPropertyName[i];
451 pdata++;
452 *pdata = 0;
453 pdata++;
454 }
456 pdata += 4;
457 for(i = 0;i < usb_os_proerty[index].dwPropertyDataLength/2;i++)
458 {
459 *pdata = usb_os_proerty[index].bPropertyData[i];
460 pdata++;
461 *pdata = 0;
462 pdata++;
463 }
464 }
465 }
466 rt_usbd_ep0_write(func->device, data, setup->wLength);
467}
468
469#ifdef __cplusplus
470}
471#endif
472
473#endif
#define RT_ASSERT(EX)
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
rt_base_t rt_ssize_t
rt_base_t rt_off_t
int rt_bool_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
rt_ubase_t rt_size_t
struct rt_list_node rt_list_t
unsigned int rt_uint32_t
#define RT_NULL
rt_uint8_t addr
rt_uint8_t dir
rt_uint8_t status
rt_uint8_t type
rt_uint16_t maxpacket
rt_uint8_t ep_addr
rt_size_t size
uintf_desc_t intf_desc
rt_list_t ep_list
rt_size_t desc_size
rt_list_t list
rt_list_t func_list
struct uconfig_descriptor cfg_desc
rt_err_t(* wakeup)(void)
rt_err_t(* suspend)(void)
rt_err_t(* ep0_send_status)(void)
rt_err_t(* ep_enable)(struct uendpoint *ep)
rt_err_t(* set_address)(rt_uint8_t address)
rt_err_t(* set_config)(rt_uint8_t address)
rt_err_t(* ep_clear_stall)(rt_uint8_t address)
rt_ssize_t(* ep_read)(rt_uint8_t address, void *buffer)
rt_err_t(* ep_disable)(struct uendpoint *ep)
rt_ssize_t(* ep_write)(rt_uint8_t address, void *buffer, rt_size_t size)
rt_err_t(* ep_set_stall)(rt_uint8_t address)
rt_ssize_t(* ep_read_prepare)(rt_uint8_t address, void *buffer, rt_size_t size)
const struct udcd_ops * ops
struct rt_device parent
struct ep_id * ep_pool
rt_uint8_t device_is_hs
uep0_stage_t stage
struct uendpoint ep0
ufunction_t(* rt_usbd_function_create)(udevice_t device)
rt_list_t list
struct urequest setup
union udev_msg::@345263171021165250214261121347301063047344353021 content
struct ep_msg ep_msg
udev_msg_type type
const char ** str
rt_list_t cfg_list
struct udevice_descriptor dev_desc
uconfig_t curr_cfg
udevice_state_t state
rt_uint8_t nr_intf
rt_list_t list
usb_os_comp_id_desc_t os_comp_id_desc
struct usb_qualifier_descriptor * dev_qualifier
const char * str_intf[MAX_INTF_STR]
uep_desc_t ep_desc
rt_err_t(* rx_indicate)(struct udevice *dev, rt_size_t size)
struct uio_request request
udep_handler_t handler
rt_list_t list
struct ep_id * id
rt_bool_t stalled
rt_uint8_t * buffer
rt_list_t request_list
rt_err_t(* sof_handler)(struct ufunction *func)
rt_err_t(* disable)(struct ufunction *func)
rt_err_t(* enable)(struct ufunction *func)
rt_list_t intf_list
ufunction_ops_t ops
udev_desc_t dev_desc
rt_list_t list
rt_bool_t enabled
struct udevice * device
ualtsetting_t curr_setting
rt_list_t setting_list
rt_uint8_t intf_num
uintf_handler_t handler
rt_uint8_t * buffer
rt_size_t remain_size
UIO_REQUEST_TYPE req_type
rt_uint16_t wLength
const char * bPropertyData
rt_uint32_t dwPropertyDataLength
const char * bPropertyName
rt_uint16_t wPropertyNameLength
struct uiad_descriptor * uiad_desc_t
struct usb_os_comp_id_descriptor * usb_os_comp_id_desc_t
struct usb_os_proerty * usb_os_proerty_t
struct uinterface_descriptor * uintf_desc_t
struct udevice_descriptor * udev_desc_t
struct uendpoint_descriptor * uep_desc_t
struct urequest * ureq_t
uep0_stage_t
udevice_state_t
struct usb_os_function_comp_id_descriptor * usb_os_func_comp_id_desc_t
ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc, ufunction_ops_t ops)
rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep)
rt_inline rt_size_t dcd_ep_write(udcd_t dcd, rt_uint8_t address, void *buffer, rt_size_t size)
rt_err_t rt_usbd_event_signal(struct udev_msg *msg)
rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf)
rt_err_t rt_usbd_sof_handler(udcd_t dcd)
rt_inline rt_err_t dcd_ep_disable(udcd_t dcd, uep_t ep)
rt_err_t(* udep_handler_t)(struct ufunction *func, rt_size_t size)
int rt_usbd_vcom_class_register(void)
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_inline rt_size_t dcd_ep_read_prepare(udcd_t dcd, rt_uint8_t address, void *buffer, rt_size_t size)
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler)
uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc)
rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep)
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func)
rt_inline rt_err_t dcd_ep0_send_status(udcd_t dcd)
ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size)
rt_err_t(* uintf_handler_t)(struct ufunction *func, ureq_t setup)
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)
int rt_usbd_ecm_class_register(void)
struct udevice * udevice_t
struct ualtsetting * ualtsetting_t
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
rt_inline rt_size_t dcd_ep_read(udcd_t dcd, rt_uint8_t address, void *buffer)
rt_inline rt_err_t dcd_ep_clear_stall(udcd_t dcd, rt_uint8_t address)
int rt_usbd_msc_class_register(void)
rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
rt_err_t rt_usbd_ep0_set_stall(udevice_t device)
rt_err_t rt_usb_device_init(void)
struct udev_msg * udev_msg_t
rt_err_t rt_usbd_reset_handler(udcd_t dcd)
rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
int rt_usbd_winusb_class_register(void)
struct ufunction * ufunction_t
#define MAX_INTF_STR
rt_err_t rt_usbd_disconnect_handler(udcd_t dcd)
rt_err_t rt_usbd_connect_handler(udcd_t dcd)
udevice_t rt_usbd_device_new(void)
rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t address)
struct ufunction_ops * ufunction_ops_t
struct uio_request * uio_request_t
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t *pfunc, rt_uint8_t ep_addr)
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))
struct udcd * udcd_t
rt_err_t rt_usbd_core_init(void)
int rt_usbd_rndis_class_register(void)
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
struct uconfig * uconfig_t
int rt_usbd_hid_class_register(void)
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_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
rt_inline rt_err_t dcd_set_config(udcd_t dcd, rt_uint8_t address)
rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
udev_msg_type
@ USB_MSG_SOF
@ USB_MSG_PLUG_OUT
@ USB_MSG_RESET
@ USB_MSG_PLUG_IN
@ USB_MSG_DATA_NOTIFY
@ USB_MSG_EP_CLEAR_FEATURE
@ USB_MSG_EP0_OUT
@ USB_MSG_SETUP_NOTIFY
udevice_t rt_usbd_find_device(udcd_t dcd)
struct uendpoint * uep_t
rt_inline rt_err_t dcd_ep_set_stall(udcd_t dcd, rt_uint8_t address)
UIO_REQUEST_TYPE
@ UIO_REQUEST_READ_FULL
@ UIO_REQUEST_WRITE
@ UIO_REQUEST_READ_BEST
rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
rt_err_t rt_usbd_ep0_clear_stall(udevice_t device)
rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor *qualifier)
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
uconfig_t rt_usbd_config_new(void)
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void *desc, rt_off_t intf_pos)
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_ep0_setup_handler(udcd_t dcd, struct urequest *setup)
struct uinterface * uintf_t
rt_inline rt_err_t dcd_ep_enable(udcd_t dcd, uep_t ep)
int rt_usbd_class_list_init(void)
rt_err_t rt_usbd_class_register(udclass_t udclass)
struct udclass * udclass_t
uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler)