espressif_tinyusb/src/class/cdc/cdc_host.c

252 lines
8.9 KiB
C
Raw Normal View History

/**************************************************************************/
/*!
@file cdc_host.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "tusb_option.h"
#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_CDC)
#define _TINY_USB_SOURCE_FILE_
2018-03-12 16:45:35 +01:00
#include "common/tusb_common.h"
#include "cdc_host.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
2018-12-10 13:01:28 +01:00
typedef struct {
uint8_t itf_num;
uint8_t itf_protocol;
cdc_acm_capability_t acm_capability;
pipe_handle_t pipe_notification, pipe_out, pipe_in;
uint8_t ep_notif;
uint8_t ep_in;
uint8_t ep_out;
} cdch_data_t;
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
2018-12-10 13:01:28 +01:00
static cdch_data_t cdch_data[CFG_TUSB_HOST_DEVICE_MAX];
2018-12-10 13:01:28 +01:00
bool tuh_cdc_mounted(uint8_t dev_addr)
{
2018-12-10 13:01:28 +01:00
cdch_data_t* cdc = &cdch_data[dev_addr-1];
return cdc->ep_in && cdc->ep_out;
}
2015-05-01 14:13:43 +02:00
bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid)
{
2018-12-09 23:55:02 +01:00
if ( !tuh_cdc_mounted(dev_addr) ) return false;
cdch_data_t const * p_cdc = &cdch_data[dev_addr-1];
switch (pipeid)
{
case CDC_PIPE_NOTIFICATION:
2018-12-10 10:58:17 +01:00
return hcd_pipe_is_busy(dev_addr, p_cdc->pipe_notification );
case CDC_PIPE_DATA_IN:
2018-12-10 10:58:17 +01:00
return hcd_pipe_is_busy(dev_addr, p_cdc->pipe_in );
case CDC_PIPE_DATA_OUT:
2018-12-10 10:58:17 +01:00
return hcd_pipe_is_busy(dev_addr, p_cdc->pipe_out );
default:
return false;
}
}
//--------------------------------------------------------------------+
// APPLICATION API (parameter validation needed)
//--------------------------------------------------------------------+
2015-05-01 14:13:43 +02:00
bool tuh_cdc_serial_is_mounted(uint8_t dev_addr)
{
// TODO consider all AT Command as serial candidate
2018-12-09 23:55:02 +01:00
return tuh_cdc_mounted(dev_addr) &&
2018-12-10 13:01:28 +01:00
(CDC_COMM_PROTOCOL_ATCOMMAND <= cdch_data[dev_addr-1].itf_protocol) &&
(cdch_data[dev_addr-1].itf_protocol <= CDC_COMM_PROTOCOL_ATCOMMAND_CDMA);
}
2015-05-01 14:13:43 +02:00
tusb_error_t tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool is_notify)
{
2018-12-09 23:55:02 +01:00
TU_ASSERT( tuh_cdc_mounted(dev_addr), TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED);
TU_ASSERT( p_data != NULL && length, TUSB_ERROR_INVALID_PARA);
pipe_handle_t pipe_out = cdch_data[dev_addr-1].pipe_out;
2018-12-10 10:58:17 +01:00
if ( hcd_pipe_is_busy(dev_addr, pipe_out) ) return TUSB_ERROR_INTERFACE_IS_BUSY;
2018-12-10 10:58:17 +01:00
return hcd_pipe_xfer(dev_addr, pipe_out, (void *) p_data, length, is_notify);
}
2015-05-01 14:13:43 +02:00
tusb_error_t tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is_notify)
{
2018-12-09 23:55:02 +01:00
TU_ASSERT( tuh_cdc_mounted(dev_addr), TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED);
TU_ASSERT( p_buffer != NULL && length, TUSB_ERROR_INVALID_PARA);
pipe_handle_t pipe_in = cdch_data[dev_addr-1].pipe_in;
2018-12-10 10:58:17 +01:00
if ( hcd_pipe_is_busy(dev_addr, pipe_in) ) return TUSB_ERROR_INTERFACE_IS_BUSY;
2018-12-10 10:58:17 +01:00
return hcd_pipe_xfer(dev_addr, pipe_in, p_buffer, length, is_notify);
}
//--------------------------------------------------------------------+
// USBH-CLASS DRIVER API
//--------------------------------------------------------------------+
void cdch_init(void)
{
2018-10-23 07:19:32 +02:00
tu_memclr(cdch_data, sizeof(cdch_data_t)*CFG_TUSB_HOST_DEVICE_MAX);
}
2018-12-10 13:25:57 +01:00
bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length)
{
2018-12-10 13:01:28 +01:00
// Only support ACM
TU_VERIFY( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass);
2018-12-10 13:01:28 +01:00
// Only support AT commands, no protocol and vendor specific commands.
TU_VERIFY(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) ||
0xff == itf_desc->bInterfaceProtocol);
uint8_t const * p_desc;
cdch_data_t * p_cdc;
2018-12-10 13:01:28 +01:00
p_desc = descriptor_next ( (uint8_t const *) itf_desc );
p_cdc = &cdch_data[dev_addr-1];
2018-12-10 13:01:28 +01:00
p_cdc->itf_num = itf_desc->bInterfaceNumber;
p_cdc->itf_protocol = itf_desc->bInterfaceProtocol; // TODO 0xff is consider as rndis candidate, other is virtual Com
//------------- Communication Interface -------------//
2018-03-23 06:32:40 +01:00
(*p_length) = sizeof(tusb_desc_interface_t);
2018-12-10 13:01:28 +01:00
// Communication Functional Descriptors
2018-07-13 12:48:26 +02:00
while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] )
2018-12-10 13:01:28 +01:00
{
if ( CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT == cdc_functional_desc_typeof(p_desc) )
2018-12-10 13:01:28 +01:00
{
// save ACM bmCapabilities
2018-03-23 06:42:30 +01:00
p_cdc->acm_capability = ((cdc_desc_func_acm_t const *) p_desc)->bmCapabilities;
}
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
}
2018-07-13 12:48:26 +02:00
if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE])
2018-12-10 13:01:28 +01:00
{
// notification endpoint if any
tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) p_desc;
2018-12-10 14:26:47 +01:00
p_cdc->pipe_notification = hcd_pipe_open(rhport, dev_addr, ep_desc);
2018-12-10 13:01:28 +01:00
p_cdc->ep_notif = ep_desc->bEndpointAddress;
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
2018-12-06 16:47:49 +01:00
TU_ASSERT(pipehandle_is_valid(p_cdc->pipe_notification));
}
//------------- Data Interface (if any) -------------//
2018-07-13 12:48:26 +02:00
if ( (TUSB_DESC_INTERFACE == p_desc[DESC_OFFSET_TYPE]) &&
2018-03-23 06:32:40 +01:00
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
{
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
// data endpoints expected to be in pairs
for(uint32_t i=0; i<2; i++)
{
2018-12-10 13:01:28 +01:00
tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *) p_desc;
TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType);
TU_ASSERT(TUSB_XFER_BULK == ep_desc->bmAttributes.xfer);
2018-12-10 13:01:28 +01:00
pipe_handle_t * p_pipe_hdl = ( ep_desc->bEndpointAddress & TUSB_DIR_IN_MASK ) ?
&p_cdc->pipe_in : &p_cdc->pipe_out;
2018-12-10 14:26:47 +01:00
(*p_pipe_hdl) = hcd_pipe_open(rhport, dev_addr, ep_desc);
2018-12-06 16:47:49 +01:00
TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl) );
2018-12-10 13:01:28 +01:00
if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN )
{
p_cdc->ep_in = ep_desc->bEndpointAddress;
}else
{
p_cdc->ep_out = ep_desc->bEndpointAddress;
}
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next( p_desc );
}
}
2018-12-07 09:07:00 +01:00
// FIXME move to seperate API : connect
tusb_control_request_t request =
{
2018-12-07 09:07:00 +01:00
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
.wValue = 0x03, // dtr on, cst on
2018-12-10 13:01:28 +01:00
.wIndex = p_cdc->itf_num,
2018-12-07 09:07:00 +01:00
.wLength = 0
};
TU_ASSERT( usbh_control_xfer(dev_addr, &request, NULL) );
2018-12-06 16:47:49 +01:00
return true;
}
2018-12-09 23:55:02 +01:00
void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
{
2018-12-09 23:40:02 +01:00
tuh_cdc_xfer_isr( dev_addr, event, 0, xferred_bytes );
}
void cdch_close(uint8_t dev_addr)
{
cdch_data_t * p_cdc = &cdch_data[dev_addr-1];
2018-12-10 10:58:17 +01:00
hcd_pipe_close(TUH_OPT_RHPORT, dev_addr, p_cdc->pipe_notification);
hcd_pipe_close(TUH_OPT_RHPORT, dev_addr, p_cdc->pipe_in);
hcd_pipe_close(TUH_OPT_RHPORT, dev_addr, p_cdc->pipe_out);
2018-10-23 07:19:32 +02:00
tu_memclr(p_cdc, sizeof(cdch_data_t));
}
#endif