Github's web interface changed line endings without asking me. Oops.
This commit is contained in:
@@ -1,167 +1,167 @@
|
|||||||
/*
|
/*
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* This file is part of the TinyUSB stack.
|
* This file is part of the TinyUSB stack.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "tusb_option.h"
|
#include "tusb_option.h"
|
||||||
|
|
||||||
#if TUSB_OPT_DEVICE_ENABLED
|
#if TUSB_OPT_DEVICE_ENABLED
|
||||||
|
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "device/usbd_pvt.h"
|
#include "device/usbd_pvt.h"
|
||||||
#include "dcd.h"
|
#include "dcd.h"
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
EDPT_CTRL_OUT = 0x00,
|
EDPT_CTRL_OUT = 0x00,
|
||||||
EDPT_CTRL_IN = 0x80
|
EDPT_CTRL_IN = 0x80
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
tusb_control_request_t request;
|
tusb_control_request_t request;
|
||||||
|
|
||||||
void* buffer;
|
void* buffer;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
uint16_t total_transferred;
|
uint16_t total_transferred;
|
||||||
uint16_t requested_len;
|
uint16_t requested_len;
|
||||||
|
|
||||||
bool (*complete_cb) (uint8_t, tusb_control_request_t const *);
|
bool (*complete_cb) (uint8_t, tusb_control_request_t const *);
|
||||||
} usbd_control_xfer_t;
|
} usbd_control_xfer_t;
|
||||||
|
|
||||||
static usbd_control_xfer_t _control_state;
|
static usbd_control_xfer_t _control_state;
|
||||||
|
|
||||||
CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
|
CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
|
||||||
|
|
||||||
void usbd_control_reset (uint8_t rhport)
|
void usbd_control_reset (uint8_t rhport)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
(void) rhport;
|
||||||
tu_varclr(&_control_state);
|
tu_varclr(&_control_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
|
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
|
||||||
{
|
{
|
||||||
// status direction is reversed to one in the setup packet
|
// status direction is reversed to one in the setup packet
|
||||||
return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0);
|
return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each transaction is up to endpoint0's max packet size
|
// Each transaction is up to endpoint0's max packet size
|
||||||
static bool start_control_data_xact(uint8_t rhport)
|
static bool start_control_data_xact(uint8_t rhport)
|
||||||
{
|
{
|
||||||
uint16_t const xact_len = tu_min16(_control_state.len - _control_state.total_transferred, CFG_TUD_ENDPOINT0_SIZE);
|
uint16_t const xact_len = tu_min16(_control_state.len - _control_state.total_transferred, CFG_TUD_ENDPOINT0_SIZE);
|
||||||
|
|
||||||
uint8_t ep_addr = EDPT_CTRL_OUT;
|
uint8_t ep_addr = EDPT_CTRL_OUT;
|
||||||
|
|
||||||
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_IN )
|
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_IN )
|
||||||
{
|
{
|
||||||
ep_addr = EDPT_CTRL_IN;
|
ep_addr = EDPT_CTRL_IN;
|
||||||
memcpy(_usbd_ctrl_buf, _control_state.buffer, xact_len);
|
memcpy(_usbd_ctrl_buf, _control_state.buffer, xact_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return dcd_edpt_xfer(rhport, ep_addr, _usbd_ctrl_buf, xact_len);
|
return dcd_edpt_xfer(rhport, ep_addr, _usbd_ctrl_buf, xact_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO may find a better way
|
// TODO may find a better way
|
||||||
void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) )
|
void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) )
|
||||||
{
|
{
|
||||||
_control_state.complete_cb = fp;
|
_control_state.complete_cb = fp;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
|
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
|
||||||
{
|
{
|
||||||
// transmitted length must be <= requested length (USB 2.0 spec: 8.5.3.1 )
|
// transmitted length must be <= requested length (USB 2.0 spec: 8.5.3.1 )
|
||||||
// FIXME: Should logic be here or in place that calls this function?
|
// FIXME: Should logic be here or in place that calls this function?
|
||||||
if(len > request->wLength)
|
if(len > request->wLength)
|
||||||
len = request->wLength;
|
len = request->wLength;
|
||||||
|
|
||||||
_control_state.request = (*request);
|
_control_state.request = (*request);
|
||||||
_control_state.buffer = buffer;
|
_control_state.buffer = buffer;
|
||||||
_control_state.total_transferred = 0;
|
_control_state.total_transferred = 0;
|
||||||
_control_state.requested_len = request->wLength;
|
_control_state.requested_len = request->wLength;
|
||||||
_control_state.len = len;
|
_control_state.len = len;
|
||||||
|
|
||||||
if ( len )
|
if ( len )
|
||||||
{
|
{
|
||||||
TU_ASSERT(buffer);
|
TU_ASSERT(buffer);
|
||||||
|
|
||||||
// Data stage
|
// Data stage
|
||||||
TU_ASSERT( start_control_data_xact(rhport) );
|
TU_ASSERT( start_control_data_xact(rhport) );
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// Status stage
|
// Status stage
|
||||||
TU_ASSERT( tud_control_status(rhport, request) );
|
TU_ASSERT( tud_control_status(rhport, request) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// callback when a transaction complete on DATA stage of control endpoint
|
// callback when a transaction complete on DATA stage of control endpoint
|
||||||
bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||||
{
|
{
|
||||||
(void) result;
|
(void) result;
|
||||||
(void) ep_addr;
|
(void) ep_addr;
|
||||||
|
|
||||||
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
|
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
|
||||||
{
|
{
|
||||||
TU_VERIFY(_control_state.buffer);
|
TU_VERIFY(_control_state.buffer);
|
||||||
memcpy(_control_state.buffer, _usbd_ctrl_buf, xferred_bytes);
|
memcpy(_control_state.buffer, _usbd_ctrl_buf, xferred_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
_control_state.total_transferred += xferred_bytes;
|
_control_state.total_transferred += xferred_bytes;
|
||||||
_control_state.buffer = ((uint8_t*)_control_state.buffer) + xferred_bytes;
|
_control_state.buffer = ((uint8_t*)_control_state.buffer) + xferred_bytes;
|
||||||
|
|
||||||
if ( (_control_state.requested_len == _control_state.total_transferred) || xferred_bytes < CFG_TUD_ENDPOINT0_SIZE )
|
if ( (_control_state.requested_len == _control_state.total_transferred) || xferred_bytes < CFG_TUD_ENDPOINT0_SIZE )
|
||||||
|
|
||||||
{
|
{
|
||||||
// DATA stage is complete
|
// DATA stage is complete
|
||||||
bool is_ok = true;
|
bool is_ok = true;
|
||||||
|
|
||||||
// invoke complete callback if set
|
// invoke complete callback if set
|
||||||
// callback can still stall control in status phase e.g out data does not make sense
|
// callback can still stall control in status phase e.g out data does not make sense
|
||||||
if ( _control_state.complete_cb )
|
if ( _control_state.complete_cb )
|
||||||
{
|
{
|
||||||
is_ok = _control_state.complete_cb(rhport, &_control_state.request);
|
is_ok = _control_state.complete_cb(rhport, &_control_state.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( is_ok )
|
if ( is_ok )
|
||||||
{
|
{
|
||||||
// Send status
|
// Send status
|
||||||
TU_ASSERT( tud_control_status(rhport, &_control_state.request) );
|
TU_ASSERT( tud_control_status(rhport, &_control_state.request) );
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// Stall both IN and OUT control endpoint
|
// Stall both IN and OUT control endpoint
|
||||||
dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
|
dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
|
||||||
dcd_edpt_stall(rhport, EDPT_CTRL_IN);
|
dcd_edpt_stall(rhport, EDPT_CTRL_IN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// More data to transfer
|
// More data to transfer
|
||||||
TU_ASSERT( start_control_data_xact(rhport) );
|
TU_ASSERT( start_control_data_xact(rhport) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user