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 | #include "tusb_option.h" |
| 28 | |
| 29 | #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_CDC) |
| 30 | |
| 31 | #include "host/usbh.h" |
| 32 | #include "host/usbh_classdriver.h" |
| 33 | |
| 34 | #include "cdc_host.h" |
| 35 | |
| 36 | //--------------------------------------------------------------------+ |
| 37 | // MACRO CONSTANT TYPEDEF |
| 38 | //--------------------------------------------------------------------+ |
| 39 | typedef struct { |
| 40 | uint8_t itf_num; |
| 41 | uint8_t itf_protocol; |
| 42 | |
| 43 | uint8_t ep_notif; |
| 44 | uint8_t ep_in; |
| 45 | uint8_t ep_out; |
| 46 | |
| 47 | cdc_acm_capability_t acm_capability; |
| 48 | |
| 49 | } cdch_data_t; |
| 50 | |
| 51 | //--------------------------------------------------------------------+ |
| 52 | // INTERNAL OBJECT & FUNCTION DECLARATION |
| 53 | //--------------------------------------------------------------------+ |
| 54 | static cdch_data_t cdch_data[CFG_TUH_DEVICE_MAX]; |
| 55 | |
| 56 | static inline cdch_data_t* get_itf(uint8_t dev_addr) |
| 57 | { |
| 58 | return &cdch_data[dev_addr-1]; |
| 59 | } |
| 60 | |
| 61 | bool tuh_cdc_mounted(uint8_t dev_addr) |
| 62 | { |
| 63 | cdch_data_t* cdc = get_itf(dev_addr); |
| 64 | return cdc->ep_in && cdc->ep_out; |
| 65 | } |
| 66 | |
| 67 | bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid) |
| 68 | { |
| 69 | if ( !tuh_cdc_mounted(dev_addr) ) return false; |
| 70 | |
| 71 | cdch_data_t const * p_cdc = get_itf(dev_addr); |
| 72 | |
| 73 | switch (pipeid) |
| 74 | { |
| 75 | case CDC_PIPE_NOTIFICATION: |
| 76 | return usbh_edpt_busy(dev_addr, p_cdc->ep_notif ); |
| 77 | |
| 78 | case CDC_PIPE_DATA_IN: |
| 79 | return usbh_edpt_busy(dev_addr, p_cdc->ep_in ); |
| 80 | |
| 81 | case CDC_PIPE_DATA_OUT: |
| 82 | return usbh_edpt_busy(dev_addr, p_cdc->ep_out ); |
| 83 | |
| 84 | default: |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | //--------------------------------------------------------------------+ |
| 90 | // APPLICATION API (parameter validation needed) |
| 91 | //--------------------------------------------------------------------+ |
| 92 | bool tuh_cdc_serial_is_mounted(uint8_t dev_addr) |
| 93 | { |
| 94 | // TODO consider all AT Command as serial candidate |
| 95 | return tuh_cdc_mounted(dev_addr) && |
| 96 | (cdch_data[dev_addr-1].itf_protocol <= CDC_COMM_PROTOCOL_ATCOMMAND_CDMA); |
| 97 | } |
| 98 | |
| 99 | bool tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool is_notify) |
| 100 | { |
| 101 | (void) is_notify; |
| 102 | TU_VERIFY( tuh_cdc_mounted(dev_addr) ); |
| 103 | TU_VERIFY( p_data != NULL && length, TUSB_ERROR_INVALID_PARA); |
| 104 | |
| 105 | uint8_t const ep_out = cdch_data[dev_addr-1].ep_out; |
| 106 | if ( usbh_edpt_busy(dev_addr, ep_out) ) return false; |
| 107 | |
| 108 | return usbh_edpt_xfer(dev_addr, ep_out, (void*)(uintptr_t) p_data, length); |
| 109 | } |
| 110 | |
| 111 | bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is_notify) |
| 112 | { |
| 113 | (void) is_notify; |
| 114 | TU_VERIFY( tuh_cdc_mounted(dev_addr) ); |
| 115 | TU_VERIFY( p_buffer != NULL && length, TUSB_ERROR_INVALID_PARA); |
| 116 | |
| 117 | uint8_t const ep_in = cdch_data[dev_addr-1].ep_in; |
| 118 | if ( usbh_edpt_busy(dev_addr, ep_in) ) return false; |
| 119 | |
| 120 | return usbh_edpt_xfer(dev_addr, ep_in, p_buffer, length); |
| 121 | } |
| 122 | |
| 123 | bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_complete_cb_t complete_cb) |
| 124 | { |
| 125 | cdch_data_t const * p_cdc = get_itf(dev_addr); |
| 126 | tusb_control_request_t const request = |
| 127 | { |
| 128 | .bmRequestType_bit = |
| 129 | { |
| 130 | .recipient = TUSB_REQ_RCPT_INTERFACE, |
| 131 | .type = TUSB_REQ_TYPE_CLASS, |
| 132 | .direction = TUSB_DIR_OUT |
| 133 | }, |
| 134 | .bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE, |
| 135 | .wValue = (rts ? 2 : 0) | (dtr ? 1 : 0), |
| 136 | .wIndex = p_cdc->itf_num, |
| 137 | .wLength = 0 |
| 138 | }; |
| 139 | |
| 140 | TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, complete_cb) ); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | //--------------------------------------------------------------------+ |
| 145 | // USBH-CLASS DRIVER API |
| 146 | //--------------------------------------------------------------------+ |
| 147 | void cdch_init(void) |
| 148 | { |
| 149 | tu_memclr(cdch_data, sizeof(cdch_data)); |
| 150 | } |
| 151 | |
| 152 | bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) |
| 153 | { |
| 154 | (void) max_len; |
| 155 | |
| 156 | // Only support ACM subclass |
| 157 | // Protocol 0xFF can be RNDIS device for windows XP |
| 158 | TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass && |
| 159 | CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass && |
| 160 | 0xFF != itf_desc->bInterfaceProtocol); |
| 161 | |
| 162 | cdch_data_t * p_cdc = get_itf(dev_addr); |
| 163 | |
| 164 | p_cdc->itf_num = itf_desc->bInterfaceNumber; |
| 165 | p_cdc->itf_protocol = itf_desc->bInterfaceProtocol; |
| 166 | |
| 167 | //------------- Communication Interface -------------// |
| 168 | uint16_t drv_len = tu_desc_len(itf_desc); |
| 169 | uint8_t const * p_desc = tu_desc_next(itf_desc); |
| 170 | |
| 171 | // Communication Functional Descriptors |
| 172 | while( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len ) |
| 173 | { |
| 174 | if ( CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT == cdc_functional_desc_typeof(p_desc) ) |
| 175 | { |
| 176 | // save ACM bmCapabilities |
| 177 | p_cdc->acm_capability = ((cdc_desc_func_acm_t const *) p_desc)->bmCapabilities; |
| 178 | } |
| 179 | |
| 180 | drv_len += tu_desc_len(p_desc); |
| 181 | p_desc = tu_desc_next(p_desc); |
| 182 | } |
| 183 | |
| 184 | if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) |
| 185 | { |
| 186 | // notification endpoint |
| 187 | tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc; |
| 188 | |
| 189 | TU_ASSERT( usbh_edpt_open(rhport, dev_addr, desc_ep) ); |
| 190 | p_cdc->ep_notif = desc_ep->bEndpointAddress; |
| 191 | |
| 192 | drv_len += tu_desc_len(p_desc); |
| 193 | p_desc = tu_desc_next(p_desc); |
| 194 | } |
| 195 | |
| 196 | //------------- Data Interface (if any) -------------// |
| 197 | if ( (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && |
| 198 | (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) |
| 199 | { |
| 200 | // next to endpoint descriptor |
| 201 | drv_len += tu_desc_len(p_desc); |
| 202 | p_desc = tu_desc_next(p_desc); |
| 203 | |
| 204 | // data endpoints expected to be in pairs |
| 205 | for(uint32_t i=0; i<2; i++) |
| 206 | { |
| 207 | tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc; |
| 208 | TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_BULK == desc_ep->bmAttributes.xfer); |
| 209 | |
| 210 | TU_ASSERT(usbh_edpt_open(rhport, dev_addr, desc_ep)); |
| 211 | |
| 212 | if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN ) |
| 213 | { |
| 214 | p_cdc->ep_in = desc_ep->bEndpointAddress; |
| 215 | }else |
| 216 | { |
| 217 | p_cdc->ep_out = desc_ep->bEndpointAddress; |
| 218 | } |
| 219 | |
| 220 | drv_len += tu_desc_len(p_desc); |
| 221 | p_desc = tu_desc_next( p_desc ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool cdch_set_config(uint8_t dev_addr, uint8_t itf_num) |
| 229 | { |
| 230 | (void) dev_addr; (void) itf_num; |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | bool cdch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) |
| 235 | { |
| 236 | (void) ep_addr; |
| 237 | tuh_cdc_xfer_isr( dev_addr, event, 0, xferred_bytes ); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | void cdch_close(uint8_t dev_addr) |
| 242 | { |
| 243 | TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX, ); |
| 244 | |
| 245 | cdch_data_t * p_cdc = get_itf(dev_addr); |
| 246 | tu_memclr(p_cdc, sizeof(cdch_data_t)); |
| 247 | } |
| 248 | |
| 249 | #endif |