usbd_core.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file usbd_core.h
  3. *
  4. * Copyright (c) 2022 sakumisu
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one or more
  7. * contributor license agreements. See the NOTICE file distributed with
  8. * this work for additional information regarding copyright ownership. The
  9. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance with the
  11. * License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. * License for the specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. #ifndef _USBD_CORE_H
  23. #define _USBD_CORE_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdbool.h>
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include "usb_config.h"
  32. #include "usb_util.h"
  33. #include "usb_errno.h"
  34. #include "usb_def.h"
  35. #include "usb_list.h"
  36. #include "usb_mem.h"
  37. #include "usb_log.h"
  38. #include "usb_dc.h"
  39. enum usbd_event_type {
  40. /** USB error reported by the controller */
  41. USBD_EVENT_ERROR,
  42. /** USB reset */
  43. USBD_EVENT_RESET,
  44. /** Start of Frame received */
  45. USBD_EVENT_SOF,
  46. /** USB connection established, hardware enumeration is completed */
  47. USBD_EVENT_CONNECTED,
  48. /** USB configuration done */
  49. USBD_EVENT_CONFIGURED,
  50. /** USB connection suspended by the HOST */
  51. USBD_EVENT_SUSPEND,
  52. /** USB connection lost */
  53. USBD_EVENT_DISCONNECTED,
  54. /** USB connection resumed by the HOST */
  55. USBD_EVENT_RESUME,
  56. /** USB interface selected */
  57. USBD_EVENT_SET_INTERFACE,
  58. /** USB interface selected */
  59. USBD_EVENT_SET_REMOTE_WAKEUP,
  60. /** USB interface selected */
  61. USBD_EVENT_CLEAR_REMOTE_WAKEUP,
  62. /** Set Feature ENDPOINT_HALT received */
  63. USBD_EVENT_SET_HALT,
  64. /** Clear Feature ENDPOINT_HALT received */
  65. USBD_EVENT_CLEAR_HALT,
  66. /** setup packet received */
  67. USBD_EVENT_SETUP_NOTIFY,
  68. /** ep0 in packet received */
  69. USBD_EVENT_EP0_IN_NOTIFY,
  70. /** ep0 out packet received */
  71. USBD_EVENT_EP0_OUT_NOTIFY,
  72. /** ep in packet except ep0 received */
  73. USBD_EVENT_EP_IN_NOTIFY,
  74. /** ep out packet except ep0 received */
  75. USBD_EVENT_EP_OUT_NOTIFY,
  76. /** Initial USB connection status */
  77. USBD_EVENT_UNKNOWN
  78. };
  79. /**
  80. * @brief Callback function signature for the USB Endpoint status
  81. */
  82. typedef void (*usbd_endpoint_callback)(uint8_t ep);
  83. /**
  84. * @brief Callback function signature for class specific requests
  85. *
  86. * Function which handles Class specific requests corresponding to an
  87. * interface number specified in the device descriptor table. For host
  88. * to device direction the 'len' and 'payload_data' contain the length
  89. * of the received data and the pointer to the received data respectively.
  90. * For device to host class requests, 'len' and 'payload_data' should be
  91. * set by the callback function with the length and the address of the
  92. * data to be transmitted buffer respectively.
  93. */
  94. typedef int (*usbd_request_handler)(struct usb_setup_packet *setup,
  95. uint8_t **data, uint32_t *transfer_len);
  96. /* callback function pointer structure for Application to handle events */
  97. typedef void (*usbd_notify_handler)(uint8_t event, void *arg);
  98. typedef struct usbd_endpoint {
  99. usb_slist_t list;
  100. uint8_t ep_addr;
  101. usbd_endpoint_callback ep_cb;
  102. } usbd_endpoint_t;
  103. typedef struct usbd_interface {
  104. usb_slist_t list;
  105. /** Handler for USB Class specific commands*/
  106. usbd_request_handler class_handler;
  107. /** Handler for USB Vendor specific commands */
  108. usbd_request_handler vendor_handler;
  109. /** Handler for USB custom specific commands */
  110. usbd_request_handler custom_handler;
  111. /** Handler for USB event notify commands */
  112. usbd_notify_handler notify_handler;
  113. uint8_t intf_num;
  114. usb_slist_t ep_list;
  115. } usbd_interface_t;
  116. typedef struct usbd_class {
  117. usb_slist_t list;
  118. const char *name;
  119. usb_slist_t intf_list;
  120. } usbd_class_t;
  121. void usbd_event_notify_handler(uint8_t event, void *arg);
  122. void usbd_desc_register(const uint8_t *desc);
  123. void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc);
  124. void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc);
  125. void usbd_bos_desc_register(struct usb_bos_descriptor *desc);
  126. void usbd_class_register(usbd_class_t *devclass);
  127. void usbd_class_add_interface(usbd_class_t *devclass, usbd_interface_t *intf);
  128. void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep);
  129. bool usb_device_is_configured(void);
  130. int usbd_initialize(void);
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif