1 /* 2 * Copyright (c) 2022, sakumisu 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef USBD_CORE_H 7 #define USBD_CORE_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #include <stdbool.h> 14 #include <string.h> 15 #include <stdint.h> 16 #include <stdlib.h> 17 18 #include "usb_config.h" 19 #include "usb_util.h" 20 #include "usb_errno.h" 21 #include "usb_def.h" 22 #include "usb_list.h" 23 #include "usb_mem.h" 24 #include "usb_log.h" 25 #include "usb_dc.h" 26 27 enum usbd_event_type { 28 /* USB DCD IRQ */ 29 USBD_EVENT_ERROR, /** USB error reported by the controller */ 30 USBD_EVENT_RESET, /** USB reset */ 31 USBD_EVENT_SOF, /** Start of Frame received */ 32 USBD_EVENT_CONNECTED, /** USB connected*/ 33 USBD_EVENT_DISCONNECTED, /** USB disconnected */ 34 USBD_EVENT_SUSPEND, /** USB connection suspended by the HOST */ 35 USBD_EVENT_RESUME, /** USB connection resumed by the HOST */ 36 37 /* USB DEVICE STATUS */ 38 USBD_EVENT_CONFIGURED, /** USB configuration done */ 39 USBD_EVENT_SET_INTERFACE, /** USB interface selected */ 40 USBD_EVENT_SET_REMOTE_WAKEUP, /** USB set remote wakeup */ 41 USBD_EVENT_CLR_REMOTE_WAKEUP, /** USB clear remote wakeup */ 42 USBD_EVENT_UNKNOWN 43 }; 44 45 typedef int (*usbd_request_handler)(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len); 46 typedef void (*usbd_endpoint_callback)(uint8_t ep, uint32_t nbytes); 47 typedef void (*usbd_notify_handler)(uint8_t event, void *arg); 48 49 struct usbd_endpoint { 50 uint8_t ep_addr; 51 usbd_endpoint_callback ep_cb; 52 }; 53 54 struct usbd_interface { 55 usbd_request_handler class_interface_handler; 56 usbd_request_handler class_endpoint_handler; 57 usbd_request_handler vendor_handler; 58 usbd_notify_handler notify_handler; 59 const uint8_t *hid_report_descriptor; 60 uint32_t hid_report_descriptor_len; 61 uint8_t intf_num; 62 }; 63 64 struct usb_descriptor { 65 const uint8_t *device_descriptor; 66 const uint8_t *fs_config_descriptor; 67 const uint8_t *hs_config_descriptor; 68 const uint8_t *device_quality_descriptor; 69 const uint8_t *fs_other_speed_descriptor; 70 const uint8_t *hs_other_speed_descriptor; 71 const char **string_descriptor; 72 struct usb_msosv1_descriptor *msosv1_descriptor; 73 struct usb_msosv2_descriptor *msosv2_descriptor; 74 struct usb_webusb_url_ex_descriptor *webusb_url_descriptor; 75 struct usb_bos_descriptor *bos_descriptor; 76 }; 77 78 #ifdef CONFIG_USBDEV_ADVANCE_DESC 79 void usbd_desc_register(struct usb_descriptor *desc); 80 #else 81 void usbd_desc_register(const uint8_t *desc); 82 void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc); 83 void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc); 84 void usbd_bos_desc_register(struct usb_bos_descriptor *desc); 85 #endif 86 87 void usbd_add_interface(struct usbd_interface *intf); 88 void usbd_add_endpoint(struct usbd_endpoint *ep); 89 90 bool usb_device_is_configured(void); 91 int usbd_initialize(void); 92 int usbd_deinitialize(void); 93 94 void usbd_event_handler(uint8_t event); 95 96 #ifdef __cplusplus 97 } 98 #endif 99 100 #endif /* USBD_CORE_H */ 101