Open FFBoard
Open source force feedback firmware
hcd.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef _TUSB_HCD_H_
28#define _TUSB_HCD_H_
29
30#include "common/tusb_common.h"
31#include "osal/osal.h"
32#include "common/tusb_fifo.h"
33
34#ifdef __cplusplus
35 extern "C" {
36#endif
37
38//--------------------------------------------------------------------+
39// Configuration
40//--------------------------------------------------------------------+
41
42// Max number of endpoints pair per device
43// TODO optimize memory usage
44#ifndef CFG_TUH_ENDPOINT_MAX
45 #define CFG_TUH_ENDPOINT_MAX 16
46// #ifdef TUP_HCD_ENDPOINT_MAX
47// #define CFG_TUH_ENDPPOINT_MAX TUP_HCD_ENDPOINT_MAX
48// #else
49// #define
50// #endif
51#endif
52
53//--------------------------------------------------------------------+
54// MACRO CONSTANT TYPEDEF
55//--------------------------------------------------------------------+
56typedef enum {
57 HCD_EVENT_DEVICE_ATTACH,
58 HCD_EVENT_DEVICE_REMOVE,
59 HCD_EVENT_XFER_COMPLETE,
60
61 USBH_EVENT_FUNC_CALL, // Not an HCD event
62 HCD_EVENT_COUNT
63} hcd_eventid_t;
64
65typedef struct {
66 uint8_t rhport;
67 uint8_t event_id;
68 uint8_t dev_addr;
69
70 union {
71 // Attach, Remove
72 struct {
73 uint8_t hub_addr;
74 uint8_t hub_port;
75 uint8_t speed;
76 } connection;
77
78 // XFER_COMPLETE
79 struct {
80 uint8_t ep_addr;
81 uint8_t result;
82 uint32_t len;
84
85 // FUNC_CALL
86 struct {
87 void (*func) (void*);
88 void* param;
89 }func_call;
90 };
92
93typedef struct {
94 uint8_t rhport;
95 uint8_t hub_addr;
96 uint8_t hub_port;
97 uint8_t speed;
99
100//--------------------------------------------------------------------+
101// Memory API
102//--------------------------------------------------------------------+
103
104// clean/flush data cache: write cache -> memory.
105// Required before an DMA TX transfer to make sure data is in memory
106bool hcd_dcache_clean(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
107
108// invalidate data cache: mark cache as invalid, next read will read from memory
109// Required BOTH before and after an DMA RX transfer
110bool hcd_dcache_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
111
112// clean and invalidate data cache
113// Required before an DMA transfer where memory is both read/write by DMA
114bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
115
116//--------------------------------------------------------------------+
117// Controller API
118//--------------------------------------------------------------------+
119
120// optional hcd configuration, called by tuh_configure()
121bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
122
123// Initialize controller to host mode
124bool hcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init);
125
126// De-initialize controller
127bool hcd_deinit(uint8_t rhport);
128
129// Interrupt Handler
130void hcd_int_handler(uint8_t rhport, bool in_isr);
131
132// Enable USB interrupt
133void hcd_int_enable (uint8_t rhport);
134
135// Disable USB interrupt
136void hcd_int_disable(uint8_t rhport);
137
138// Get frame number (1ms)
139uint32_t hcd_frame_number(uint8_t rhport);
140
141//--------------------------------------------------------------------+
142// Port API
143//--------------------------------------------------------------------+
144
145// Get the current connect status of roothub port
146bool hcd_port_connect_status(uint8_t rhport);
147
148// Reset USB bus on the port. Return immediately, bus reset sequence may not be complete.
149// Some port would require hcd_port_reset_end() to be invoked after 10ms to complete the reset sequence.
150void hcd_port_reset(uint8_t rhport);
151
152// Complete bus reset sequence, may be required by some controllers
153void hcd_port_reset_end(uint8_t rhport);
154
155// Get port link speed
156tusb_speed_t hcd_port_speed_get(uint8_t rhport);
157
158// HCD closes all opened endpoints belong to this device
159void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
160
161//--------------------------------------------------------------------+
162// Endpoints API
163//--------------------------------------------------------------------+
164
165// Open an endpoint
166bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * ep_desc);
167
168// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
169bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
170
171// Abort a queued transfer. Note: it can only abort transfer that has not been started
172// Return true if a queued transfer is aborted, false if there is no transfer to abort
173bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
174
175// Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked
176bool hcd_setup_send(uint8_t rhport, uint8_t daddr, uint8_t const setup_packet[8]);
177
178// clear stall, data toggle is also reset to DATA0
179bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
180
181//--------------------------------------------------------------------+
182// USBH implemented API
183//--------------------------------------------------------------------+
184
185// Get device tree information of a device
186// USB device tree can be complicated and manged by USBH, this help HCD to retrieve
187// needed topology info to carry out its work
188extern void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info);
189
190//------------- Event API -------------//
191
192// Called by HCD to notify stack
193extern void hcd_event_handler(hcd_event_t const* event, bool in_isr);
194
195// Helper to send device attach event
196TU_ATTR_ALWAYS_INLINE static inline
197void hcd_event_device_attach(uint8_t rhport, bool in_isr) {
198 hcd_event_t event;
199 event.rhport = rhport;
200 event.event_id = HCD_EVENT_DEVICE_ATTACH;
201 event.connection.hub_addr = 0;
202 event.connection.hub_port = 0;
203
204 hcd_event_handler(&event, in_isr);
205}
206
207// Helper to send device removal event
208TU_ATTR_ALWAYS_INLINE static inline
209void hcd_event_device_remove(uint8_t rhport, bool in_isr) {
210 hcd_event_t event;
211 event.rhport = rhport;
212 event.event_id = HCD_EVENT_DEVICE_REMOVE;
213 event.connection.hub_addr = 0;
214 event.connection.hub_port = 0;
215
216 hcd_event_handler(&event, in_isr);
217}
218
219// Helper to send USB transfer event
220TU_ATTR_ALWAYS_INLINE static inline
221void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr) {
222 hcd_event_t event = {
223 .rhport = 0, // TODO correct rhport
224 .event_id = HCD_EVENT_XFER_COMPLETE,
225 .dev_addr = dev_addr,
226 };
227 event.xfer_complete.ep_addr = ep_addr;
228 event.xfer_complete.result = result;
229 event.xfer_complete.len = xferred_bytes;
230
231 hcd_event_handler(&event, in_isr);
232}
233
234#ifdef __cplusplus
235 }
236#endif
237
238#endif /* _TUSB_HCD_H_ */
static bool in_isr
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
static void xfer_complete(xfer_ctl_t *xfer, uint8_t result, bool in_isr)
Definition: dcd_pic32mz.c:296
bool hcd_dcache_invalidate(void const *addr, uint32_t data_size) TU_ATTR_WEAK
Definition: hcd_ci_hs.c:49
static TU_ATTR_ALWAYS_INLINE void hcd_event_device_remove(uint8_t rhport, bool in_isr)
Definition: hcd.h:209
static TU_ATTR_ALWAYS_INLINE void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr)
Definition: hcd.h:221
void hcd_int_disable(uint8_t rhport)
Definition: hcd_max3421.c:577
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param)
Definition: usbh.c:55
bool hcd_dcache_clean_invalidate(void const *addr, uint32_t data_size) TU_ATTR_WEAK
Definition: hcd_ci_hs.c:53
bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const *ep_desc)
Definition: hcd_max3421.c:625
bool hcd_dcache_clean(void const *addr, uint32_t data_size) TU_ATTR_WEAK
Definition: hcd_ci_hs.c:45
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr)
Definition: hcd_max3421.c:797
bool hcd_deinit(uint8_t rhport)
Definition: usbh.c:50
void hcd_int_enable(uint8_t rhport)
Definition: hcd_max3421.c:571
void hcd_device_close(uint8_t rhport, uint8_t dev_addr)
Definition: hcd_max3421.c:615
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr)
Definition: hcd_max3421.c:759
static TU_ATTR_ALWAYS_INLINE void hcd_event_device_attach(uint8_t rhport, bool in_isr)
Definition: hcd.h:197
void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t *devtree_info)
Definition: usbh.c:949
void hcd_port_reset_end(uint8_t rhport)
Definition: hcd_max3421.c:604
void hcd_event_handler(hcd_event_t const *event, bool in_isr)
Definition: usbh.c:964
bool hcd_setup_send(uint8_t rhport, uint8_t daddr, uint8_t const setup_packet[8])
Definition: hcd_max3421.c:775
void hcd_port_reset(uint8_t rhport)
Definition: hcd_max3421.c:599
bool hcd_port_connect_status(uint8_t rhport)
Definition: hcd_max3421.c:592
uint32_t hcd_frame_number(uint8_t rhport)
Definition: hcd_max3421.c:582
tusb_speed_t hcd_port_speed_get(uint8_t rhport)
Definition: hcd_max3421.c:609
bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t *buffer, uint16_t buflen)
Definition: hcd_max3421.c:733
void hcd_int_handler(uint8_t rhport, bool in_isr)
Definition: hcd_max3421.c:1002
bool hcd_init(uint8_t rhport, const tusb_rhport_init_t *rh_init)
Definition: hcd_max3421.c:497
uint8_t const * buffer
Definition: midi_device.h:100
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint8_t hub_port
Definition: hcd.h:96
uint8_t hub_addr
Definition: hcd.h:95
uint8_t speed
Definition: hcd.h:97
uint8_t rhport
Definition: hcd.h:94
uint8_t rhport
Definition: hcd.h:66
void * param
Definition: hcd.h:88
uint8_t ep_addr
Definition: hcd.h:80
uint8_t speed
Definition: hcd.h:75
uint8_t hub_port
Definition: hcd.h:74
uint8_t hub_addr
Definition: hcd.h:73
uint32_t len
Definition: hcd.h:82
uint8_t result
Definition: hcd.h:81
uint8_t dev_addr
Definition: hcd.h:68
uint8_t event_id
Definition: hcd.h:67
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition: tusb_types.h:49
xfer_result_t
Definition: tusb_types.h:236
uint8_t daddr
Definition: usbh.c:264