Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief USB Device Stack Core Layer Definition. |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | */ |
| 32 | |
| 33 | #ifndef _USB_USBDC_H_ |
| 34 | #define _USB_USBDC_H_ |
| 35 | |
| 36 | #include "usb_includes.h" |
| 37 | #include "usb_protocol.h" |
| 38 | #include "hal_usb_device.h" |
| 39 | #include "usbd_config.h" |
| 40 | |
| 41 | /** USB device states. */ |
| 42 | enum usbd_state { |
| 43 | USBD_S_OFF = 0, |
| 44 | USBD_S_POWER = 1, |
| 45 | USBD_S_DEFAULT = 2, |
| 46 | USBD_S_ADDRESS = 3, |
| 47 | USBD_S_CONFIG = 4, |
| 48 | USBD_S_SUSPEND = 0x10 |
| 49 | }; |
| 50 | |
| 51 | /** USB device core handler type. */ |
| 52 | enum usbdc_handler_type { USBDC_HDL_SOF, USBDC_HDL_REQ, USBDC_HDL_CHANGE }; |
| 53 | |
| 54 | /** USB device core change notification type. */ |
| 55 | enum usbdc_change_type { |
| 56 | /** Change of connection, detected by vbus. */ |
| 57 | USBDC_C_CONN, |
| 58 | /** Change of state, by RESET, SetAddress(), SetConfig(). */ |
| 59 | USBDC_C_STATE, |
| 60 | /** Change of power. */ |
| 61 | USBDC_C_POWER, |
| 62 | /** Change of remote wakeup setting. */ |
| 63 | USBDC_C_REMOTE_WAKEUP |
| 64 | }; |
| 65 | |
| 66 | /** Power change. */ |
| 67 | enum usbdc_power_type { USBDC_ACTIVE, USBDC_SLEEP, USBDC_SUSPEND }; |
| 68 | |
| 69 | /** USB device general function control code. */ |
| 70 | enum usbdf_control { |
| 71 | /** Enable the function. |
| 72 | * int32_t ctrl(usbdf, USBDF_ENABLE, struct usbd_descriptors *desc); |
| 73 | * Parameter holds interface descriptor and |
| 74 | * configuration descriptor end position. |
| 75 | */ |
| 76 | USBDF_ENABLE, |
| 77 | /** Disable the function. |
| 78 | * int32_t ctrl(usbdf, USBDF_DISABLE, struct usbd_descriptors *desc); |
| 79 | * Parameter holds interface descriptor and |
| 80 | * configuration descriptor end position. |
| 81 | * Input NULL to force disable the function anyway. |
| 82 | */ |
| 83 | USBDF_DISABLE, |
| 84 | /** Get interface alternate setting. |
| 85 | * int32_t ctrl(usbdf, USBDF_GET_IFACE, struct usb_req *req); |
| 86 | * Parameter holds interface number who should return |
| 87 | * the alternate setting. |
| 88 | */ |
| 89 | USBDF_GET_IFACE |
| 90 | }; |
| 91 | |
| 92 | /** Describes a list of USB descriptors. */ |
| 93 | struct usbd_descriptors { |
| 94 | /** Pointer to Start of Descriptors. */ |
| 95 | uint8_t *sod; |
| 96 | /** Pointer to End of Descriptors. */ |
| 97 | uint8_t *eod; |
| 98 | }; |
| 99 | |
| 100 | /** Describes the USB device core descriptors. */ |
| 101 | struct usbdc_descriptors { |
| 102 | struct usbd_descriptors *ls_fs; |
| 103 | #if CONF_USBD_HS_SP |
| 104 | struct usbd_descriptors *hs; |
| 105 | #endif |
| 106 | }; |
| 107 | |
| 108 | /** Describes a list of core handler descriptor. */ |
| 109 | struct usbdc_handler { |
| 110 | /** Pointer to next handler. */ |
| 111 | struct usbdc_handler *next; |
| 112 | /** Pointer to handler function. */ |
| 113 | FUNC_PTR func; |
| 114 | }; |
| 115 | |
| 116 | /** Forward declaration for USB device function driver. */ |
| 117 | struct usbdf_driver; |
| 118 | |
| 119 | /** SOF handling function. */ |
| 120 | typedef void (*usbdc_sof_cb_t)(void); |
| 121 | |
| 122 | /** REQ handling function. */ |
| 123 | typedef int32_t (*usbdc_req_cb_t)(uint8_t ep, struct usb_req *req, enum usb_ctrl_stage stage); |
| 124 | |
| 125 | /** Change notification callback function. */ |
| 126 | typedef void (*usbdc_change_cb_t)(enum usbdc_change_type change, uint32_t value); |
| 127 | |
| 128 | /** Control function for USB device general function driver. */ |
| 129 | typedef int32_t (*usbdf_control_cb_t)(struct usbdf_driver *drv, enum usbdf_control ctrl, void *param); |
| 130 | |
| 131 | /** USB device general function driver descriptor. */ |
| 132 | struct usbdf_driver { |
| 133 | /** Pointer to next function.*/ |
| 134 | struct usbdf_driver *next; |
| 135 | /** Pointer to control function.*/ |
| 136 | usbdf_control_cb_t ctrl; |
| 137 | /** Pointer to function driver specific data. */ |
| 138 | void *func_data; |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * \brief Register the handler |
| 143 | * \param[in] type USB device core handler type. |
| 144 | * \param[in] h Pointer to usb device core handler. |
| 145 | */ |
| 146 | void usbdc_register_handler(enum usbdc_handler_type type, const struct usbdc_handler *h); |
| 147 | |
| 148 | /** |
| 149 | * \brief Unregister the handler |
| 150 | * \param[in] type USB device core handler type. |
| 151 | * \param[in] h Pointer to usb device core handler. |
| 152 | */ |
| 153 | void usbdc_unregister_handler(enum usbdc_handler_type type, const struct usbdc_handler *h); |
| 154 | |
| 155 | /** |
| 156 | * \brief Initialize the USB device core driver |
| 157 | * \param[in] ctrl_buf Pointer to a buffer to be used by usb device ctrl endpoint |
| 158 | * Note: the size of ctrl_buf should not be less than the size of EP0 |
| 159 | * \return Operation status. |
| 160 | */ |
| 161 | int32_t usbdc_init(uint8_t *ctrl_buf); |
| 162 | |
| 163 | /** |
| 164 | * \brief Deinitialize the USB device core driver |
| 165 | * \return Operation status. |
| 166 | */ |
| 167 | int32_t usbdc_deinit(void); |
| 168 | |
| 169 | /** |
| 170 | * \brief Register function support of a USB device function |
| 171 | * \param[in] func Pointer to usb device function driver structure |
| 172 | */ |
| 173 | void usbdc_register_function(struct usbdf_driver *func); |
| 174 | |
| 175 | /** |
| 176 | * \brief Unregister function support of a USB device function |
| 177 | * \param[in] func Pointer to usb device function driver structure |
| 178 | */ |
| 179 | void usbdc_unregister_function(struct usbdf_driver *func); |
| 180 | |
| 181 | /** |
| 182 | * \brief Validate the descriptors |
| 183 | * \param[in] desces Pointer to usb device core descriptors |
| 184 | * \return Operation status. |
| 185 | */ |
| 186 | int32_t usbdc_validate_desces(struct usbd_descriptors *desces); |
| 187 | |
| 188 | /** |
| 189 | * \brief Issue USB device data transfer |
| 190 | * \param[in] ep endpointer address. |
| 191 | * \param[in] buf Pointer to data transfer buffer. |
| 192 | * \param[in] size the size of data transfer. |
| 193 | * \param[in] zlp flag to indicate zero length packet. |
| 194 | * \return Operation status. |
| 195 | */ |
| 196 | int32_t usbdc_xfer(uint8_t ep, uint8_t *buf, uint32_t size, bool zlp); |
| 197 | |
| 198 | /** |
| 199 | * \brief Start the USB device driver with specific descriptors set |
| 200 | * \param[in] desces Pointer to usb device core descriptors (FS/LS), |
| 201 | * or pointer to array of core descriptors (HS), the |
| 202 | * very first one includes device descriptor, FS/LS |
| 203 | * configuration descriptor, string descriptor, and |
| 204 | * may include device qualifier and other speed |
| 205 | * configuration descriptor; the second one includes |
| 206 | * high speed used descriptors. |
| 207 | * Note that string descriptor should be included in |
| 208 | * first place. |
| 209 | * \return Operation status. |
| 210 | */ |
| 211 | int32_t usbdc_start(struct usbd_descriptors *desces); |
| 212 | |
| 213 | /** |
| 214 | * \brief Stop the USB device driver |
| 215 | * \return Operation status. |
| 216 | */ |
| 217 | int32_t usbdc_stop(void); |
| 218 | |
| 219 | /** |
| 220 | * \brief Attach the USB device to host |
| 221 | */ |
| 222 | void usbdc_attach(void); |
| 223 | |
| 224 | /** |
| 225 | * \brief Detach the USB device from host |
| 226 | */ |
| 227 | void usbdc_detach(void); |
| 228 | |
| 229 | /** |
| 230 | * \brief Send remote wakeup to host |
| 231 | */ |
| 232 | void usbdc_remotewakeup(void); |
| 233 | |
| 234 | /** |
| 235 | * \brief Return USB device ctrl end pointer buffer start address |
| 236 | */ |
| 237 | uint8_t *usbdc_get_ctrl_buffer(void); |
| 238 | |
| 239 | /** |
| 240 | * \brief Return current USB state |
| 241 | */ |
| 242 | uint8_t usbdc_get_state(void); |
| 243 | |
| 244 | /** |
| 245 | * \brief Return version |
| 246 | */ |
| 247 | uint32_t usbdc_get_version(void); |
| 248 | |
| 249 | #endif /* USBDC_H_ */ |