Open FFBoard
Open source force feedback firmware
hid_host.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_HID_HOST_H_
28#define _TUSB_HID_HOST_H_
29
30#include "hid.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36//--------------------------------------------------------------------+
37// Class Driver Configuration
38//--------------------------------------------------------------------+
39
40// TODO Highspeed interrupt can be up to 512 bytes
41#ifndef CFG_TUH_HID_EPIN_BUFSIZE
42#define CFG_TUH_HID_EPIN_BUFSIZE 64
43#endif
44
45#ifndef CFG_TUH_HID_EPOUT_BUFSIZE
46#define CFG_TUH_HID_EPOUT_BUFSIZE 64
47#endif
48
49
50typedef struct {
51 uint8_t report_id;
52 uint8_t usage;
53 uint16_t usage_page;
54
55 // TODO still use the endpoint size for now
56// uint8_t in_len; // length of IN report
57// uint8_t out_len; // length of OUT report
59
60//--------------------------------------------------------------------+
61// Interface API
62//--------------------------------------------------------------------+
63
64// Get the total number of mounted HID interfaces of a device
65uint8_t tuh_hid_itf_get_count(uint8_t dev_addr);
66
67// Get all mounted interfaces across devices
68uint8_t tuh_hid_itf_get_total_count(void);
69
70// backward compatible rename
71#define tuh_hid_instance_count tuh_hid_itf_get_count
72
73// Get Interface information
74bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* itf_info);
75
76// Get Interface index from device address + interface number
77// return TUSB_INDEX_INVALID_8 (0xFF) if not found
78uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num);
79
80// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
81uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t idx);
82
83// Check if HID interface is mounted
84bool tuh_hid_mounted(uint8_t dev_addr, uint8_t idx);
85
86// Parse report descriptor into array of report_info struct and return number of reports.
87// For complicated report, application should write its own parser.
88TU_ATTR_UNUSED uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr, uint8_t arr_count,
89 uint8_t const* desc_report, uint16_t desc_len);
90
91//--------------------------------------------------------------------+
92// Control Endpoint API
93//--------------------------------------------------------------------+
94
95// Get current protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
96// Note: Device will be initialized in Boot protocol for simplicity.
97// Application can use set_protocol() to switch back to Report protocol.
98uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t idx);
99
100// Device by default is enumerated in Boot protocol for simplicity. Application
101// can use this to modify the default protocol for next enumeration.
102void tuh_hid_set_default_protocol(uint8_t protocol);
103
104// Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
105// This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE)
106bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
107
108// Get Report using control endpoint
109// report_type is either Input, Output or Feature, (value from hid_report_type_t)
110bool tuh_hid_get_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
111
112// Set Report using control endpoint
113// report_type is either Input, Output or Feature, (value from hid_report_type_t)
114bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type,
115 void* report, uint16_t len);
116
117//--------------------------------------------------------------------+
118// Interrupt Endpoint API
119//--------------------------------------------------------------------+
120
121// Check if HID interface is ready to receive report
122bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx);
123
124// Try to receive next report on Interrupt Endpoint. Immediately return
125// - true If succeeded, tuh_hid_report_received_cb() callback will be invoked when report is available
126// - false if failed to queue the transfer e.g endpoint is busy
127bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t idx);
128
129// Abort receiving report on Interrupt Endpoint
130bool tuh_hid_receive_abort(uint8_t dev_addr, uint8_t idx);
131
132// Check if HID interface is ready to send report
133bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx);
134
135// Send report using interrupt endpoint
136// If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent.
137bool tuh_hid_send_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len);
138
139//--------------------------------------------------------------------+
140// Callbacks (Weak is optional)
141//--------------------------------------------------------------------+
142
143// Invoked when device with hid interface is mounted
144// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
145// can be used to parse common/simple enough descriptor.
146// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
147// therefore report_desc = NULL, desc_len = 0
148TU_ATTR_WEAK void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report_desc, uint16_t desc_len);
149
150// Invoked when device with hid interface is un-mounted
151TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t idx);
152
153// Invoked when received report from device via interrupt endpoint
154// Note: if there is report ID (composite), it is 1st byte of report
155void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
156
157// Invoked when sent report to device successfully via interrupt endpoint
158TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
159
160// Invoked when Get Report to device via either control endpoint
161// len = 0 indicate there is error in the transfer e.g stalled response
162TU_ATTR_WEAK void tuh_hid_get_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len);
163
164// Invoked when Sent Report to device via either control endpoint
165// len = 0 indicate there is error in the transfer e.g stalled response
166TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len);
167
168// Invoked when Set Protocol request is complete
169TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
170
171//--------------------------------------------------------------------+
172// Internal Class Driver API
173//--------------------------------------------------------------------+
174bool hidh_init(void);
175bool hidh_deinit(void);
176bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const* desc_itf, uint16_t max_len);
177bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num);
178bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
179void hidh_close(uint8_t dev_addr);
180
181#ifdef __cplusplus
182}
183#endif
184
185#endif /* _TUSB_HID_HOST_H_ */
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
bool tuh_hid_mounted(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:119
uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num)
Definition: hid_host.c:147
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, const void *report, uint16_t len)
Definition: hid_host.c:379
bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
Definition: hid_host.c:429
uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:156
uint8_t tuh_hid_itf_get_count(uint8_t dev_addr)
Definition: hid_host.c:103
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len)
TU_ATTR_WEAK void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t idx, uint8_t const *report_desc, uint16_t desc_len)
bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:373
TU_ATTR_UNUSED uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t *reports_info_arr, uint8_t arr_count, uint8_t const *desc_report, uint16_t desc_len)
Definition: hid_host.c:627
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void *report, uint16_t len)
Definition: hid_host.c:284
bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:347
bool hidh_init(void)
Definition: hid_host.c:419
void hidh_close(uint8_t dev_addr)
Definition: hid_host.c:451
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol)
Definition: hid_host.c:218
bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t *itf_info)
Definition: hid_host.c:125
bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
Definition: hid_host.c:537
TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const *report, uint16_t len)
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:164
bool tuh_hid_receive_abort(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:367
void tuh_hid_set_default_protocol(uint8_t protocol)
Definition: hid_host.c:186
uint8_t tuh_hid_itf_get_total_count(void)
Definition: hid_host.c:111
bool hidh_deinit(void)
Definition: hid_host.c:425
TU_ATTR_WEAK void tuh_hid_get_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len)
bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t idx)
Definition: hid_host.c:353
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t idx, uint8_t const *report, uint16_t len)
TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t idx)
bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
Definition: hid_host.c:466
bool tuh_hid_get_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void *report, uint16_t len)
Definition: hid_host.c:240
TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol)
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint16_t usage_page
Definition: hid_host.h:53
xfer_result_t
Definition: tusb_types.h:236
uint8_t daddr
Definition: usbh.c:264