2012-02-07 08:12:55 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_USB_HID USB COM Functions
|
|
|
|
* @brief PIOS USB COM implementation for HID interfaces
|
|
|
|
* @notes This implements serial emulation over HID reports
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_usb_hid.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief USB COM functions (STM32 dependent code)
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-06-05 00:54:08 +02:00
|
|
|
#include <pios.h>
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-03-15 19:42:54 +01:00
|
|
|
#ifdef PIOS_INCLUDE_USB_HID
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-06-05 00:54:08 +02:00
|
|
|
#include <pios_usb_hid_priv.h>
|
2012-02-07 08:12:55 +01:00
|
|
|
#include "pios_usb_board_data.h" /* PIOS_BOARD_*_DATA_LENGTH */
|
2013-06-05 00:54:08 +02:00
|
|
|
#include <pios_usbhook.h> /* PIOS_USBHOOK_* */
|
|
|
|
#include <pios_delay.h>
|
2012-02-07 08:12:55 +01:00
|
|
|
static void PIOS_USB_HID_RegisterTxCallback(uint32_t usbhid_id, pios_com_callback tx_out_cb, uint32_t context);
|
|
|
|
static void PIOS_USB_HID_RegisterRxCallback(uint32_t usbhid_id, pios_com_callback rx_in_cb, uint32_t context);
|
|
|
|
static void PIOS_USB_HID_TxStart(uint32_t usbhid_id, uint16_t tx_bytes_avail);
|
|
|
|
static void PIOS_USB_HID_RxStart(uint32_t usbhid_id, uint16_t rx_bytes_avail);
|
2016-06-22 01:37:42 +02:00
|
|
|
static uint32_t PIOS_USB_HID_Available(uint32_t usbhid_id);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
const struct pios_com_driver pios_usb_hid_com_driver = {
|
2013-05-19 16:37:30 +02:00
|
|
|
.tx_start = PIOS_USB_HID_TxStart,
|
|
|
|
.rx_start = PIOS_USB_HID_RxStart,
|
|
|
|
.bind_tx_cb = PIOS_USB_HID_RegisterTxCallback,
|
|
|
|
.bind_rx_cb = PIOS_USB_HID_RegisterRxCallback,
|
2016-06-22 01:37:42 +02:00
|
|
|
.available = PIOS_USB_HID_Available,
|
2012-02-07 08:12:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum pios_usb_hid_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USB_HID_DEV_MAGIC = 0xAA00BB00,
|
2012-02-07 08:12:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_usb_hid_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_usb_hid_dev_magic magic;
|
|
|
|
const struct pios_usb_hid_cfg *cfg;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t lower_id;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_com_callback rx_in_cb;
|
|
|
|
uint32_t rx_in_context;
|
|
|
|
pios_com_callback tx_out_cb;
|
|
|
|
uint32_t tx_out_context;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool usb_if_enabled;
|
2012-03-06 04:57:46 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t rx_packet_buffer[PIOS_USB_BOARD_HID_DATA_LENGTH] __attribute__((aligned(4)));
|
2013-06-06 10:15:15 +02:00
|
|
|
volatile bool rx_active;
|
2012-03-06 05:01:11 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t tx_packet_buffer[PIOS_USB_BOARD_HID_DATA_LENGTH] __attribute__((aligned(4)));
|
2013-06-06 10:15:15 +02:00
|
|
|
volatile bool tx_active;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t rx_dropped;
|
|
|
|
uint32_t rx_oversize;
|
2012-02-07 08:12:55 +01:00
|
|
|
};
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool PIOS_USB_HID_validate(struct pios_usb_hid_dev *usb_hid_dev)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return usb_hid_dev && (usb_hid_dev->magic == PIOS_USB_HID_DEV_MAGIC);
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_usb_hid_dev *PIOS_USB_HID_alloc(void)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2014-06-11 20:11:48 +02:00
|
|
|
usb_hid_dev = (struct pios_usb_hid_dev *)pios_malloc(sizeof(struct pios_usb_hid_dev));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usb_hid_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
memset(usb_hid_dev, 0, sizeof(struct pios_usb_hid_dev));
|
|
|
|
usb_hid_dev->magic = PIOS_USB_HID_DEV_MAGIC;
|
|
|
|
return usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static struct pios_usb_hid_dev pios_usb_hid_devs[PIOS_USB_HID_MAX_DEVS];
|
|
|
|
static uint8_t pios_usb_hid_num_devs;
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_usb_hid_dev *PIOS_USB_HID_alloc(void)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_usb_hid_num_devs >= PIOS_USB_HID_MAX_DEVS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
usb_hid_dev = &pios_usb_hid_devs[pios_usb_hid_num_devs++];
|
2013-03-15 03:57:57 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
memset(usb_hid_dev, 0, sizeof(struct pios_usb_hid_dev));
|
|
|
|
usb_hid_dev->magic = PIOS_USB_HID_DEV_MAGIC;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_FREERTOS) */
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
static void PIOS_USB_HID_IF_Init(uint32_t usb_hid_id);
|
|
|
|
static void PIOS_USB_HID_IF_DeInit(uint32_t usb_hid_id);
|
|
|
|
static bool PIOS_USB_HID_IF_Setup(uint32_t usb_hid_id, struct usb_setup_request *req);
|
2013-02-11 14:11:12 +01:00
|
|
|
static void PIOS_USB_HID_IF_CtrlDataOut(uint32_t usb_hid_id, const struct usb_setup_request *req);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
static struct pios_usb_ifops usb_hid_ifops = {
|
2013-05-19 16:37:30 +02:00
|
|
|
.init = PIOS_USB_HID_IF_Init,
|
|
|
|
.deinit = PIOS_USB_HID_IF_DeInit,
|
|
|
|
.setup = PIOS_USB_HID_IF_Setup,
|
|
|
|
.ctrl_data_out = PIOS_USB_HID_IF_CtrlDataOut,
|
2012-02-07 08:12:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool PIOS_USB_HID_EP_IN_Callback(uint32_t usb_hid_id, uint8_t epnum, uint16_t len);
|
|
|
|
static bool PIOS_USB_HID_EP_OUT_Callback(uint32_t usb_hid_id, uint8_t epnum, uint16_t len);
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_USB_HID_Init(uint32_t *usbhid_id, const struct pios_usb_hid_cfg *cfg, uint32_t lower_id)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(usbhid_id);
|
|
|
|
PIOS_Assert(cfg);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
usb_hid_dev = (struct pios_usb_hid_dev *)PIOS_USB_HID_alloc();
|
|
|
|
if (!usb_hid_dev) {
|
|
|
|
goto out_fail;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Bind the configuration to the device instance */
|
|
|
|
usb_hid_dev->cfg = cfg;
|
|
|
|
usb_hid_dev->lower_id = lower_id;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Rx and Tx are not active yet */
|
|
|
|
usb_hid_dev->rx_active = false;
|
|
|
|
usb_hid_dev->tx_active = false;
|
2012-03-06 05:01:11 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Register class specific interface callbacks with the USBHOOK layer */
|
|
|
|
usb_hid_dev->usb_if_enabled = false;
|
|
|
|
PIOS_USBHOOK_RegisterIfOps(cfg->data_if, &usb_hid_ifops, (uint32_t)usb_hid_dev);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
*usbhid_id = (uint32_t)usb_hid_dev;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
out_fail:
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-05 01:43:56 +01:00
|
|
|
static struct pios_usbhook_descriptor hid_desc;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_USB_HID_RegisterHidDescriptor(const uint8_t *desc, uint16_t length)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
hid_desc.descriptor = desc;
|
|
|
|
hid_desc.length = length;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pios_usbhook_descriptor hid_report_desc;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_USB_HID_RegisterHidReport(const uint8_t *desc, uint16_t length)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
hid_report_desc.descriptor = desc;
|
|
|
|
hid_report_desc.length = length;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool PIOS_USB_HID_SendReport(struct pios_usb_hid_dev *usb_hid_dev)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t bytes_to_tx;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usb_hid_dev->tx_out_cb) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-06 10:15:15 +02:00
|
|
|
READ_MEMORY_BARRIER();
|
2013-05-19 16:37:30 +02:00
|
|
|
bool need_yield = false;
|
2012-02-07 08:12:55 +01:00
|
|
|
#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE
|
2013-05-19 16:37:30 +02:00
|
|
|
bytes_to_tx = (usb_hid_dev->tx_out_cb)(usb_hid_dev->tx_out_context,
|
|
|
|
&usb_hid_dev->tx_packet_buffer[1],
|
|
|
|
sizeof(usb_hid_dev->tx_packet_buffer) - 1,
|
|
|
|
NULL,
|
|
|
|
&need_yield);
|
2012-02-07 08:12:55 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
bytes_to_tx = (usb_hid_dev->tx_out_cb)(usb_hid_dev->tx_out_context,
|
|
|
|
&usb_hid_dev->tx_packet_buffer[2],
|
|
|
|
sizeof(usb_hid_dev->tx_packet_buffer) - 2,
|
|
|
|
NULL,
|
|
|
|
&need_yield);
|
2012-02-07 08:12:55 +01:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
if (bytes_to_tx == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* Mark this endpoint as being tx active _before_ actually transmitting
|
|
|
|
* to make sure we don't race with the Tx completion interrupt
|
|
|
|
*/
|
|
|
|
usb_hid_dev->tx_active = true;
|
2012-03-06 05:01:11 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Always set type as report ID */
|
|
|
|
usb_hid_dev->tx_packet_buffer[0] = 1;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USBHOOK_EndpointTx(usb_hid_dev->cfg->data_tx_ep,
|
|
|
|
usb_hid_dev->tx_packet_buffer,
|
2013-06-05 19:01:01 +02:00
|
|
|
sizeof(usb_hid_dev->tx_packet_buffer));
|
2012-02-07 08:12:55 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
usb_hid_dev->tx_packet_buffer[1] = bytes_to_tx;
|
|
|
|
PIOS_USBHOOK_EndpointTx(usb_hid_dev->cfg->data_tx_ep,
|
|
|
|
usb_hid_dev->tx_packet_buffer,
|
2013-06-05 19:01:01 +02:00
|
|
|
sizeof(usb_hid_dev->tx_packet_buffer));
|
2012-02-07 08:12:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (need_yield) {
|
2013-09-05 05:26:23 +02:00
|
|
|
vPortYield();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
#endif /* PIOS_INCLUDE_FREERTOS */
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return true;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void PIOS_USB_HID_RxStart(uint32_t usbhid_id, uint16_t rx_bytes_avail)
|
|
|
|
{
|
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usbhid_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USB_HID_validate(usb_hid_dev);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Make sure this USB interface has been initialized */
|
|
|
|
if (!usb_hid_dev->usb_if_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-06 04:57:46 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!PIOS_USB_CheckAvailable(usb_hid_dev->lower_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// If endpoint was stalled and there is now space make it valid
|
2012-02-07 08:12:55 +01:00
|
|
|
#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 1;
|
2012-02-07 08:12:55 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 2;
|
2012-02-07 08:12:55 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usb_hid_dev->rx_active && (rx_bytes_avail >= max_payload_length)) {
|
|
|
|
PIOS_USBHOOK_EndpointRx(usb_hid_dev->cfg->data_rx_ep,
|
|
|
|
usb_hid_dev->rx_packet_buffer,
|
|
|
|
sizeof(usb_hid_dev->rx_packet_buffer));
|
|
|
|
usb_hid_dev->rx_active = true;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void PIOS_USB_HID_TxStart(uint32_t usbhid_id, __attribute__((unused)) uint16_t tx_bytes_avail)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usbhid_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USB_HID_validate(usb_hid_dev);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Make sure this USB interface has been initialized */
|
|
|
|
if (!usb_hid_dev->usb_if_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!PIOS_USB_CheckAvailable(usb_hid_dev->lower_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usb_hid_dev->tx_active) {
|
|
|
|
/* Transmitter is not currently active, send a report */
|
|
|
|
PIOS_USB_HID_SendReport(usb_hid_dev);
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PIOS_USB_HID_RegisterRxCallback(uint32_t usbhid_id, pios_com_callback rx_in_cb, uint32_t context)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usbhid_id;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool valid = PIOS_USB_HID_validate(usb_hid_dev);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
|
|
|
usb_hid_dev->rx_in_context = context;
|
2013-06-06 01:39:45 +02:00
|
|
|
WRITE_MEMORY_BARRIER();
|
2013-05-19 16:37:30 +02:00
|
|
|
usb_hid_dev->rx_in_cb = rx_in_cb;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PIOS_USB_HID_RegisterTxCallback(uint32_t usbhid_id, pios_com_callback tx_out_cb, uint32_t context)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usbhid_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USB_HID_validate(usb_hid_dev);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
|
|
|
usb_hid_dev->tx_out_context = context;
|
2013-06-06 01:39:45 +02:00
|
|
|
WRITE_MEMORY_BARRIER();
|
2013-05-19 16:37:30 +02:00
|
|
|
usb_hid_dev->tx_out_cb = tx_out_cb;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PIOS_USB_HID_IF_Init(uint32_t usb_hid_id)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usb_hid_id;
|
|
|
|
|
|
|
|
if (!PIOS_USB_HID_validate(usb_hid_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register endpoint specific callbacks with the USBHOOK layer */
|
|
|
|
PIOS_USBHOOK_RegisterEpInCallback(usb_hid_dev->cfg->data_tx_ep,
|
|
|
|
sizeof(usb_hid_dev->tx_packet_buffer),
|
|
|
|
PIOS_USB_HID_EP_IN_Callback,
|
|
|
|
(uint32_t)usb_hid_dev);
|
|
|
|
PIOS_USBHOOK_RegisterEpOutCallback(usb_hid_dev->cfg->data_rx_ep,
|
|
|
|
sizeof(usb_hid_dev->rx_packet_buffer),
|
|
|
|
PIOS_USB_HID_EP_OUT_Callback,
|
|
|
|
(uint32_t)usb_hid_dev);
|
|
|
|
usb_hid_dev->usb_if_enabled = true;
|
2013-06-04 01:52:44 +02:00
|
|
|
usb_hid_dev->tx_active = false;
|
|
|
|
usb_hid_dev->rx_active = false;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PIOS_USB_HID_IF_DeInit(uint32_t usb_hid_id)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usb_hid_id;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!PIOS_USB_HID_validate(usb_hid_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* DeRegister endpoint specific callbacks with the USBHOOK layer */
|
|
|
|
usb_hid_dev->usb_if_enabled = false;
|
2013-06-04 01:52:44 +02:00
|
|
|
usb_hid_dev->tx_active = false;
|
|
|
|
usb_hid_dev->rx_active = false;
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USBHOOK_DeRegisterEpInCallback(usb_hid_dev->cfg->data_tx_ep);
|
|
|
|
PIOS_USBHOOK_DeRegisterEpOutCallback(usb_hid_dev->cfg->data_rx_ep);
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t hid_protocol;
|
|
|
|
static uint8_t hid_altset;
|
2013-03-17 04:39:43 +01:00
|
|
|
|
|
|
|
struct hid_idle_msg {
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t idle_period;
|
|
|
|
uint8_t report_id;
|
2013-03-17 04:39:43 +01:00
|
|
|
};
|
|
|
|
static struct hid_idle_msg hid_idle;
|
2013-03-16 04:59:58 +01:00
|
|
|
static uint8_t dummy_report[2];
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
static bool PIOS_USB_HID_IF_Setup(uint32_t usb_hid_id, struct usb_setup_request *req)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usb_hid_id;
|
|
|
|
|
|
|
|
if (!PIOS_USB_HID_validate(usb_hid_dev)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure this is a request for an interface we know about */
|
|
|
|
uint8_t ifnum = req->wIndex & 0xFF;
|
|
|
|
if (ifnum != usb_hid_dev->cfg->data_if) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req->bmRequestType & (USB_REQ_TYPE_MASK | USB_REQ_RECIPIENT_MASK)) {
|
|
|
|
case (USB_REQ_TYPE_STANDARD | USB_REQ_RECIPIENT_INTERFACE):
|
|
|
|
switch (req->bRequest) {
|
|
|
|
case USB_REQ_GET_DESCRIPTOR:
|
|
|
|
switch (req->wValue >> 8) {
|
|
|
|
case USB_DESC_TYPE_REPORT:
|
|
|
|
PIOS_USBHOOK_CtrlTx(hid_report_desc.descriptor,
|
|
|
|
MIN(hid_report_desc.length, req->wLength));
|
|
|
|
break;
|
|
|
|
case USB_DESC_TYPE_HID:
|
|
|
|
PIOS_USBHOOK_CtrlTx(hid_desc.descriptor,
|
|
|
|
MIN(hid_desc.length, req->wLength));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unhandled descriptor request */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_REQ_GET_INTERFACE:
|
|
|
|
PIOS_USBHOOK_CtrlTx(&hid_altset, 1);
|
|
|
|
break;
|
|
|
|
case USB_REQ_SET_INTERFACE:
|
|
|
|
hid_altset = (uint8_t)(req->wValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unhandled standard request */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (USB_REQ_TYPE_CLASS | USB_REQ_RECIPIENT_INTERFACE):
|
|
|
|
switch (req->bRequest) {
|
|
|
|
case USB_HID_REQ_SET_PROTOCOL:
|
|
|
|
hid_protocol = (uint8_t)(req->wValue);
|
|
|
|
break;
|
|
|
|
case USB_HID_REQ_SET_IDLE:
|
|
|
|
/* Idle rates are currently ignored but decoded for debugging */
|
|
|
|
hid_idle.idle_period = req->wValue & 0xFF00 >> 8;
|
|
|
|
hid_idle.report_id = req->wValue & 0x00FF;
|
|
|
|
break;
|
|
|
|
case USB_HID_REQ_GET_PROTOCOL:
|
|
|
|
PIOS_USBHOOK_CtrlTx(&hid_protocol, 1);
|
|
|
|
break;
|
|
|
|
case USB_HID_REQ_GET_REPORT:
|
|
|
|
/* Give back a dummy input report */
|
|
|
|
dummy_report[0] = req->wValue & 0xFF; /* Report ID */
|
|
|
|
dummy_report[1] = 0x00; /* dummy value */
|
|
|
|
PIOS_USBHOOK_CtrlTx(dummy_report,
|
|
|
|
MIN(sizeof(dummy_report), req->wLength));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unhandled class request */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unhandled request */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
2013-02-11 14:11:12 +01:00
|
|
|
static void PIOS_USB_HID_IF_CtrlDataOut(__attribute__((unused)) uint32_t usb_hid_id, __attribute__((unused)) const struct usb_setup_request *req)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* HID devices don't have any OUT data stages on the control endpoint */
|
|
|
|
PIOS_Assert(0);
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback used to indicate a transmission from device INto host completed
|
|
|
|
* Checks if any data remains, pads it into HID packet and sends.
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static bool PIOS_USB_HID_EP_IN_Callback(uint32_t usb_hid_id, __attribute__((unused)) uint8_t epnum, __attribute__((unused)) uint16_t len)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usb_hid_id;
|
|
|
|
|
|
|
|
if (!PIOS_USB_HID_validate(usb_hid_dev)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_USB_CheckAvailable(usb_hid_dev->lower_id) &&
|
|
|
|
PIOS_USB_HID_SendReport(usb_hid_dev)) {
|
|
|
|
/* More data has been queued, leave tx_active set to true */
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* Nothing new sent, transmitter is now inactive */
|
|
|
|
usb_hid_dev->tx_active = false;
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EP1 OUT Callback Routine
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static bool PIOS_USB_HID_EP_OUT_Callback(uint32_t usb_hid_id, __attribute__((unused)) uint8_t epnum, uint16_t len)
|
2012-02-07 08:12:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usb_hid_dev *usb_hid_dev = (struct pios_usb_hid_dev *)usb_hid_id;
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!PIOS_USB_HID_validate(usb_hid_dev)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (len > sizeof(usb_hid_dev->rx_packet_buffer)) {
|
|
|
|
len = sizeof(usb_hid_dev->rx_packet_buffer);
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usb_hid_dev->rx_in_cb) {
|
|
|
|
/* No Rx call back registered, disable the receiver */
|
|
|
|
usb_hid_dev->rx_active = false;
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-06 10:15:15 +02:00
|
|
|
READ_MEMORY_BARRIER();
|
2013-05-19 16:37:30 +02:00
|
|
|
/* The first byte is report ID (not checked), the second byte is the valid data length */
|
|
|
|
uint16_t headroom;
|
|
|
|
bool need_yield = false;
|
2012-02-07 08:12:55 +01:00
|
|
|
#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE
|
2013-05-19 16:37:30 +02:00
|
|
|
(usb_hid_dev->rx_in_cb)(usb_hid_dev->rx_in_context,
|
|
|
|
&usb_hid_dev->rx_packet_buffer[1],
|
|
|
|
len - 1,
|
|
|
|
&headroom,
|
|
|
|
&need_yield);
|
2012-02-07 08:12:55 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
(usb_hid_dev->rx_in_cb)(usb_hid_dev->rx_in_context,
|
|
|
|
&usb_hid_dev->rx_packet_buffer[2],
|
|
|
|
usb_hid_dev->rx_packet_buffer[1],
|
|
|
|
&headroom,
|
|
|
|
&need_yield);
|
2012-02-07 08:12:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 1;
|
2012-02-07 08:12:55 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 2;
|
2012-02-07 08:12:55 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool rc;
|
|
|
|
if (headroom >= max_payload_length) {
|
|
|
|
/* We have room for a maximum length message */
|
|
|
|
PIOS_USBHOOK_EndpointRx(usb_hid_dev->cfg->data_rx_ep,
|
|
|
|
usb_hid_dev->rx_packet_buffer,
|
|
|
|
sizeof(usb_hid_dev->rx_packet_buffer));
|
|
|
|
rc = true;
|
|
|
|
} else {
|
|
|
|
/* Not enough room left for a message, apply backpressure */
|
|
|
|
usb_hid_dev->rx_active = false;
|
|
|
|
rc = false;
|
|
|
|
}
|
2012-02-07 08:12:55 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (need_yield) {
|
2013-09-05 05:26:23 +02:00
|
|
|
vPortYield();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
#endif /* PIOS_INCLUDE_FREERTOS */
|
2012-03-27 05:36:22 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return rc;
|
2012-02-07 08:12:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 01:37:42 +02:00
|
|
|
static uint32_t PIOS_USB_HID_Available(uint32_t usbhid_id)
|
|
|
|
{
|
|
|
|
return PIOS_USB_CheckAvailable(usbhid_id) ? COM_AVAILABLE_RXTX : COM_AVAILABLE_NONE;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:42:54 +01:00
|
|
|
#endif /* PIOS_INCLUDE_USB_HID */
|