usbd_core.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file usbd_core.h
  3. *
  4. * Copyright (c) 2021 Bouffalolab team
  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 "usb_util.h"
  28. #include "usb_def.h"
  29. #include "usb_dc.h"
  30. enum usb_event_type {
  31. /** USB error reported by the controller */
  32. USB_EVENT_ERROR,
  33. /** USB reset */
  34. USB_EVENT_RESET,
  35. /** Start of Frame received */
  36. USB_EVENT_SOF,
  37. /** USB connection established, hardware enumeration is completed */
  38. USB_EVENT_CONNECTED,
  39. /** USB configuration done */
  40. USB_EVENT_CONFIGURED,
  41. /** USB connection suspended by the HOST */
  42. USB_EVENT_SUSPEND,
  43. /** USB connection lost */
  44. USB_EVENT_DISCONNECTED,
  45. /** USB connection resumed by the HOST */
  46. USB_EVENT_RESUME,
  47. /** USB interface selected */
  48. USB_EVENT_SET_INTERFACE,
  49. /** USB interface selected */
  50. USB_EVENT_SET_REMOTE_WAKEUP,
  51. /** USB interface selected */
  52. USB_EVENT_CLEAR_REMOTE_WAKEUP,
  53. /** Set Feature ENDPOINT_HALT received */
  54. USB_EVENT_SET_HALT,
  55. /** Clear Feature ENDPOINT_HALT received */
  56. USB_EVENT_CLEAR_HALT,
  57. /** setup packet received */
  58. USB_EVENT_SETUP_NOTIFY,
  59. /** ep0 in packet received */
  60. USB_EVENT_EP0_IN_NOTIFY,
  61. /** ep0 out packet received */
  62. USB_EVENT_EP0_OUT_NOTIFY,
  63. /** ep in packet except ep0 received */
  64. USB_EVENT_EP_IN_NOTIFY,
  65. /** ep out packet except ep0 received */
  66. USB_EVENT_EP_OUT_NOTIFY,
  67. /** Initial USB connection status */
  68. USB_EVENT_UNKNOWN
  69. };
  70. /**
  71. * @brief Callback function signature for the USB Endpoint status
  72. */
  73. typedef void (*usbd_endpoint_callback)(uint8_t ep);
  74. /**
  75. * @brief Callback function signature for class specific requests
  76. *
  77. * Function which handles Class specific requests corresponding to an
  78. * interface number specified in the device descriptor table. For host
  79. * to device direction the 'len' and 'payload_data' contain the length
  80. * of the received data and the pointer to the received data respectively.
  81. * For device to host class requests, 'len' and 'payload_data' should be
  82. * set by the callback function with the length and the address of the
  83. * data to be transmitted buffer respectively.
  84. */
  85. typedef int (*usbd_request_handler)(struct usb_setup_packet *setup,
  86. uint8_t **data, uint32_t *transfer_len);
  87. /* callback function pointer structure for Application to handle events */
  88. typedef void (*usbd_notify_handler)(uint8_t event, void *arg);
  89. typedef struct usbd_endpoint {
  90. usb_slist_t list;
  91. uint8_t ep_addr;
  92. usbd_endpoint_callback ep_cb;
  93. } usbd_endpoint_t;
  94. typedef struct usbd_interface {
  95. usb_slist_t list;
  96. /** Handler for USB Class specific Control (EP 0) communications */
  97. usbd_request_handler class_handler;
  98. /** Handler for USB Vendor specific commands */
  99. usbd_request_handler vendor_handler;
  100. /** Handler for USB custom specific commands */
  101. usbd_request_handler custom_handler;
  102. /** Handler for USB event notify commands */
  103. usbd_notify_handler notify_handler;
  104. uint8_t intf_num;
  105. usb_slist_t ep_list;
  106. } usbd_interface_t;
  107. typedef struct usbd_class {
  108. usb_slist_t list;
  109. const char *name;
  110. usb_slist_t intf_list;
  111. } usbd_class_t;
  112. void usbd_event_notify_handler(uint8_t event, void *arg);
  113. void usbd_desc_register(const uint8_t *desc);
  114. void usbd_class_register(usbd_class_t *class);
  115. void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc);
  116. void usbd_class_add_interface(usbd_class_t *class, usbd_interface_t *intf);
  117. void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep);
  118. bool usb_device_is_configured(void);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif