Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 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 |
| 33 | extern "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 | |
| 50 | typedef struct |
| 51 | { |
| 52 | uint8_t report_id; |
| 53 | uint8_t usage; |
| 54 | uint16_t usage_page; |
| 55 | |
| 56 | // TODO still use the endpoint size for now |
| 57 | // uint8_t in_len; // length of IN report |
| 58 | // uint8_t out_len; // length of OUT report |
| 59 | } tuh_hid_report_info_t; |
| 60 | |
| 61 | //--------------------------------------------------------------------+ |
| 62 | // Interface API |
| 63 | //--------------------------------------------------------------------+ |
| 64 | |
| 65 | // Get the number of HID instances |
| 66 | uint8_t tuh_hid_instance_count(uint8_t dev_addr); |
| 67 | |
| 68 | // Check if HID instance is mounted |
| 69 | bool tuh_hid_mounted(uint8_t dev_addr, uint8_t instance); |
| 70 | |
| 71 | // Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values |
| 72 | uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t instance); |
| 73 | |
| 74 | // Parse report descriptor into array of report_info struct and return number of reports. |
| 75 | // For complicated report, application should write its own parser. |
| 76 | 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) TU_ATTR_UNUSED; |
| 77 | |
| 78 | //--------------------------------------------------------------------+ |
| 79 | // Control Endpoint API |
| 80 | //--------------------------------------------------------------------+ |
| 81 | |
| 82 | // Get current protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1) |
| 83 | // Note: Device will be initialized in Boot protocol for simplicity. |
| 84 | // Application can use set_protocol() to switch back to Report protocol. |
| 85 | uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance); |
| 86 | |
| 87 | // Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1) |
| 88 | // This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE) |
| 89 | bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol); |
| 90 | |
| 91 | // Set Report using control endpoint |
| 92 | // report_type is either Intput, Output or Feature, (value from hid_report_type_t) |
| 93 | bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len); |
| 94 | |
| 95 | //--------------------------------------------------------------------+ |
| 96 | // Interrupt Endpoint API |
| 97 | //--------------------------------------------------------------------+ |
| 98 | |
| 99 | // Check if the interface is ready to use |
| 100 | //bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance); |
| 101 | |
| 102 | // Try to receive next report on Interrupt Endpoint. Immediately return |
| 103 | // - true If succeeded, tuh_hid_report_received_cb() callback will be invoked when report is available |
| 104 | // - false if failed to queue the transfer e.g endpoint is busy |
| 105 | bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance); |
| 106 | |
| 107 | // Send report using interrupt endpoint |
| 108 | // If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent. |
| 109 | //void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len); |
| 110 | |
| 111 | //--------------------------------------------------------------------+ |
| 112 | // Callbacks (Weak is optional) |
| 113 | //--------------------------------------------------------------------+ |
| 114 | |
| 115 | // Invoked when device with hid interface is mounted |
| 116 | // Report descriptor is also available for use. tuh_hid_parse_report_descriptor() |
| 117 | // can be used to parse common/simple enough descriptor. |
| 118 | // Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped |
| 119 | // therefore report_desc = NULL, desc_len = 0 |
| 120 | void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report_desc, uint16_t desc_len); |
| 121 | |
| 122 | // Invoked when device with hid interface is un-mounted |
| 123 | TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance); |
| 124 | |
| 125 | // Invoked when received report from device via interrupt endpoint |
| 126 | // Note: if there is report ID (composite), it is 1st byte of report |
| 127 | void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); |
| 128 | |
| 129 | // Invoked when sent report to device successfully via interrupt endpoint |
| 130 | TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); |
| 131 | |
| 132 | // Invoked when Sent Report to device via either control endpoint |
| 133 | // len = 0 indicate there is error in the transfer e.g stalled response |
| 134 | TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); |
| 135 | |
| 136 | // Invoked when Set Protocol request is complete |
| 137 | TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t protocol); |
| 138 | |
| 139 | //--------------------------------------------------------------------+ |
| 140 | // Internal Class Driver API |
| 141 | //--------------------------------------------------------------------+ |
| 142 | void hidh_init (void); |
| 143 | bool hidh_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); |
| 144 | bool hidh_set_config (uint8_t dev_addr, uint8_t itf_num); |
| 145 | bool hidh_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); |
| 146 | void hidh_close (uint8_t dev_addr); |
| 147 | |
| 148 | #ifdef __cplusplus |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | #endif /* _TUSB_HID_HOST_H_ */ |