2019-03-20 16:11:42 +07:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018, hathach (tinyusb.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* 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
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
2014-03-10 13:13:13 +07:00
|
|
|
|
|
|
|
#include "tusb_option.h"
|
|
|
|
|
2018-07-28 12:38:45 +07:00
|
|
|
#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_HID)
|
2014-03-10 13:13:13 +07:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// INCLUDE
|
|
|
|
//--------------------------------------------------------------------+
|
2018-03-12 22:45:35 +07:00
|
|
|
#include "common/tusb_common.h"
|
2014-03-10 13:13:13 +07:00
|
|
|
#include "hid_device.h"
|
2018-03-22 14:38:09 +07:00
|
|
|
#include "device/usbd_pvt.h"
|
2014-03-10 13:13:13 +07:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
//--------------------------------------------------------------------+
|
2018-07-23 15:25:45 +07:00
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
#ifndef CFG_TUD_HID_BUFSIZE
|
|
|
|
#define CFG_TUD_HID_BUFSIZE 16
|
|
|
|
#endif
|
2018-07-31 13:31:03 +07:00
|
|
|
|
2018-07-30 17:01:20 +07:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-07-23 15:25:45 +07:00
|
|
|
uint8_t itf_num;
|
|
|
|
uint8_t ep_in;
|
2019-05-01 17:05:25 +07:00
|
|
|
uint8_t ep_out; // optional
|
2019-04-18 12:32:19 +07:00
|
|
|
uint8_t boot_protocol; // Boot mouse or keyboard
|
2019-05-01 17:05:25 +07:00
|
|
|
bool boot_mode; // default = false (Report)
|
|
|
|
uint8_t idle_rate; // up to application to handle idle rate
|
2019-04-18 11:52:23 +07:00
|
|
|
uint16_t reprot_desc_len;
|
2018-07-30 17:01:20 +07:00
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
CFG_TUSB_MEM_ALIGN uint8_t report_buf[CFG_TUD_HID_BUFSIZE];
|
2014-03-10 13:13:13 +07:00
|
|
|
}hidd_interface_t;
|
|
|
|
|
2019-04-18 00:36:17 +07:00
|
|
|
CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2018-07-31 13:31:03 +07:00
|
|
|
/*------------- Helpers -------------*/
|
2018-07-28 13:50:46 +07:00
|
|
|
static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num)
|
|
|
|
{
|
2019-04-18 00:36:17 +07:00
|
|
|
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
|
2018-07-30 17:01:20 +07:00
|
|
|
{
|
|
|
|
if ( itf_num == _hidd_itf[i].itf_num ) return &_hidd_itf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2018-07-28 13:50:46 +07:00
|
|
|
}
|
|
|
|
|
2018-07-28 12:38:45 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2019-04-18 13:24:07 +07:00
|
|
|
// APPLICATION API
|
2018-07-28 12:38:45 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2019-04-18 00:36:17 +07:00
|
|
|
bool tud_hid_ready(void)
|
2018-07-28 12:38:45 +07:00
|
|
|
{
|
2019-04-18 12:32:19 +07:00
|
|
|
uint8_t itf = 0;
|
|
|
|
uint8_t const ep_in = _hidd_itf[itf].ep_in;
|
2019-04-18 11:52:23 +07:00
|
|
|
return tud_ready() && (ep_in != 0) && !dcd_edpt_busy(TUD_OPT_RHPORT, ep_in);
|
2018-07-28 12:38:45 +07:00
|
|
|
}
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2019-04-18 00:36:17 +07:00
|
|
|
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
|
2018-07-31 13:31:03 +07:00
|
|
|
{
|
2019-04-18 13:24:07 +07:00
|
|
|
TU_VERIFY( tud_hid_ready() && (len < CFG_TUD_HID_BUFSIZE) );
|
2018-07-31 13:31:03 +07:00
|
|
|
|
2019-04-18 12:32:19 +07:00
|
|
|
uint8_t itf = 0;
|
|
|
|
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
2018-07-31 13:31:03 +07:00
|
|
|
|
|
|
|
// If report id = 0, skip ID field
|
|
|
|
if (report_id)
|
|
|
|
{
|
|
|
|
p_hid->report_buf[0] = report_id;
|
|
|
|
memcpy(p_hid->report_buf+1, report, len);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
memcpy(p_hid->report_buf, report, len);
|
|
|
|
}
|
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, len + (report_id ? 1 : 0) );
|
2018-07-31 13:31:03 +07:00
|
|
|
}
|
|
|
|
|
2019-04-18 11:52:23 +07:00
|
|
|
bool tud_hid_boot_mode(void)
|
|
|
|
{
|
2019-04-18 12:32:19 +07:00
|
|
|
uint8_t itf = 0;
|
|
|
|
return _hidd_itf[itf].boot_mode;
|
2019-04-18 11:52:23 +07:00
|
|
|
}
|
|
|
|
|
2014-03-10 13:13:13 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2019-04-18 13:24:07 +07:00
|
|
|
// KEYBOARD API
|
2014-03-10 13:13:13 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2019-04-18 13:24:07 +07:00
|
|
|
bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2019-04-18 13:24:07 +07:00
|
|
|
hid_keyboard_report_t report;
|
2018-07-25 15:58:52 +07:00
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
report.modifier = modifier;
|
2018-07-23 17:46:07 +07:00
|
|
|
|
2018-07-24 18:18:02 +07:00
|
|
|
if ( keycode )
|
|
|
|
{
|
|
|
|
memcpy(report.keycode, keycode, 6);
|
|
|
|
}else
|
|
|
|
{
|
2018-10-23 12:19:32 +07:00
|
|
|
tu_memclr(report.keycode, 6);
|
2018-07-24 18:18:02 +07:00
|
|
|
}
|
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
return tud_hid_report(report_id, &report, sizeof(report));
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
2018-07-23 17:46:07 +07:00
|
|
|
|
2014-03-10 13:13:13 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MOUSE APPLICATION API
|
|
|
|
//--------------------------------------------------------------------+
|
2019-05-01 16:53:35 +07:00
|
|
|
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal)
|
2018-07-24 20:51:50 +07:00
|
|
|
{
|
|
|
|
hid_mouse_report_t report =
|
|
|
|
{
|
2019-04-18 13:24:07 +07:00
|
|
|
.buttons = buttons,
|
|
|
|
.x = x,
|
|
|
|
.y = y,
|
2019-05-01 16:53:35 +07:00
|
|
|
.wheel = vertical,
|
|
|
|
.pan = horizontal
|
2018-07-24 20:51:50 +07:00
|
|
|
};
|
|
|
|
|
2019-04-18 13:24:07 +07:00
|
|
|
return tud_hid_report(report_id, &report, sizeof(report));
|
2018-07-24 20:51:50 +07:00
|
|
|
}
|
|
|
|
|
2018-07-23 15:25:45 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// USBD-CLASS API
|
|
|
|
//--------------------------------------------------------------------+
|
2014-03-10 13:13:13 +07:00
|
|
|
void hidd_init(void)
|
|
|
|
{
|
2018-07-23 15:25:45 +07:00
|
|
|
hidd_reset(TUD_OPT_RHPORT);
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
|
|
|
|
2018-07-13 16:09:26 +07:00
|
|
|
void hidd_reset(uint8_t rhport)
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2019-03-29 01:34:53 +07:00
|
|
|
(void) rhport;
|
2018-10-23 12:19:32 +07:00
|
|
|
tu_memclr(_hidd_itf, sizeof(_hidd_itf));
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:00:59 +07:00
|
|
|
bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t *p_len)
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2018-07-23 15:25:45 +07:00
|
|
|
uint8_t const *p_desc = (uint8_t const *) desc_itf;
|
|
|
|
|
|
|
|
//------------- HID descriptor -------------//
|
2018-12-12 13:00:59 +07:00
|
|
|
p_desc = tu_desc_next(p_desc);
|
2018-07-23 15:25:45 +07:00
|
|
|
tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
|
2018-12-12 13:00:59 +07:00
|
|
|
TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType);
|
2018-07-23 15:25:45 +07:00
|
|
|
|
|
|
|
//------------- Endpoint Descriptor -------------//
|
2018-12-12 13:00:59 +07:00
|
|
|
p_desc = tu_desc_next(p_desc);
|
2018-07-23 15:25:45 +07:00
|
|
|
tusb_desc_endpoint_t const *desc_edpt = (tusb_desc_endpoint_t const *) p_desc;
|
2018-12-12 13:00:59 +07:00
|
|
|
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_edpt->bDescriptorType);
|
2018-07-28 12:38:45 +07:00
|
|
|
|
2019-04-18 11:52:23 +07:00
|
|
|
TU_ASSERT(dcd_edpt_open(rhport, desc_edpt));
|
2018-07-30 17:01:20 +07:00
|
|
|
|
2019-04-18 11:52:23 +07:00
|
|
|
// TODO support multiple HID interface
|
2019-04-18 12:32:19 +07:00
|
|
|
uint8_t itf = 0;
|
|
|
|
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2019-04-18 11:52:23 +07:00
|
|
|
if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->boot_protocol = desc_itf->bInterfaceProtocol;
|
2018-07-30 17:01:20 +07:00
|
|
|
|
2019-04-18 11:52:23 +07:00
|
|
|
p_hid->boot_mode = false; // default mode is REPORT
|
2018-07-30 17:01:20 +07:00
|
|
|
p_hid->itf_num = desc_itf->bInterfaceNumber;
|
|
|
|
p_hid->ep_in = desc_edpt->bEndpointAddress;
|
2019-04-18 11:52:23 +07:00
|
|
|
p_hid->reprot_desc_len = desc_hid->wReportLength;
|
2018-07-30 17:01:20 +07:00
|
|
|
|
|
|
|
*p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
|
|
|
|
|
2018-12-12 13:00:59 +07:00
|
|
|
return true;
|
2018-07-23 15:25:45 +07:00
|
|
|
}
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
// Handle class control request
|
|
|
|
// return false to stall control endpoint (e.g unsupported request)
|
|
|
|
bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
|
2018-07-23 15:25:45 +07:00
|
|
|
{
|
2018-07-28 13:50:46 +07:00
|
|
|
hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex );
|
2018-11-16 21:56:39 +07:00
|
|
|
TU_ASSERT(p_hid);
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2018-03-11 21:05:27 +07:00
|
|
|
if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
//------------- STD Request -------------//
|
2018-08-23 20:09:28 +07:00
|
|
|
uint8_t const desc_type = tu_u16_high(p_request->wValue);
|
|
|
|
uint8_t const desc_index = tu_u16_low (p_request->wValue);
|
2014-03-10 13:13:13 +07:00
|
|
|
(void) desc_index;
|
|
|
|
|
2018-03-22 14:38:09 +07:00
|
|
|
if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
|
|
|
|
{
|
2019-04-18 23:59:21 +07:00
|
|
|
usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.hid_report, p_hid->reprot_desc_len);
|
2018-03-22 14:38:09 +07:00
|
|
|
}else
|
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
return false; // stall unsupported request
|
2018-03-22 14:38:09 +07:00
|
|
|
}
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
2018-03-11 21:05:27 +07:00
|
|
|
else if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS)
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
//------------- Class Specific Request -------------//
|
|
|
|
switch( p_request->bRequest )
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
case HID_REQ_CONTROL_GET_REPORT:
|
2018-07-23 15:25:45 +07:00
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
// wValue = Report Type | Report ID
|
|
|
|
uint8_t const report_type = tu_u16_high(p_request->wValue);
|
|
|
|
uint8_t const report_id = tu_u16_low(p_request->wValue);
|
|
|
|
|
2019-04-18 00:36:17 +07:00
|
|
|
uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength);
|
2018-11-16 21:56:39 +07:00
|
|
|
TU_ASSERT( xferlen > 0 );
|
2019-04-18 00:36:17 +07:00
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
usbd_control_xfer(rhport, p_request, p_hid->report_buf, xferlen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HID_REQ_CONTROL_SET_REPORT:
|
|
|
|
usbd_control_xfer(rhport, p_request, p_hid->report_buf, p_request->wLength);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HID_REQ_CONTROL_SET_IDLE:
|
|
|
|
p_hid->idle_rate = tu_u16_high(p_request->wValue);
|
2019-05-01 16:53:35 +07:00
|
|
|
if ( tud_hid_set_idle_cb )
|
|
|
|
{
|
|
|
|
// stall request if callback return false
|
|
|
|
if ( !tud_hid_set_idle_cb(p_hid->idle_rate) ) return false;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
usbd_control_status(rhport, p_request);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HID_REQ_CONTROL_GET_IDLE:
|
|
|
|
// TODO idle rate of report
|
|
|
|
usbd_control_xfer(rhport, p_request, &p_hid->idle_rate, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HID_REQ_CONTROL_GET_PROTOCOL:
|
2018-07-23 15:25:45 +07:00
|
|
|
{
|
2019-04-18 11:52:23 +07:00
|
|
|
uint8_t protocol = 1-p_hid->boot_mode; // 0 is Boot, 1 is Report protocol
|
2018-11-16 21:56:39 +07:00
|
|
|
usbd_control_xfer(rhport, p_request, &protocol, 1);
|
2018-07-23 15:25:45 +07:00
|
|
|
}
|
2018-11-16 21:56:39 +07:00
|
|
|
break;
|
2014-03-10 13:13:13 +07:00
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
case HID_REQ_CONTROL_SET_PROTOCOL:
|
2019-04-18 11:52:23 +07:00
|
|
|
p_hid->boot_mode = 1 - p_request->wValue; // 0 is Boot, 1 is Report protocol
|
2019-04-18 12:32:19 +07:00
|
|
|
|
2019-05-01 16:53:35 +07:00
|
|
|
if (tud_hid_boot_mode_cb) tud_hid_boot_mode_cb(p_hid->boot_mode);
|
2019-04-18 12:32:19 +07:00
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
usbd_control_status(rhport, p_request);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: return false; // stall unsupported request
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
return false; // stall unsupported request
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
2018-11-16 21:56:39 +07:00
|
|
|
|
|
|
|
return true;
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
// Invoked when class request DATA stage is finished.
|
|
|
|
// return false to stall control endpoint (e.g Host send non-sense DATA)
|
|
|
|
bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request)
|
2018-11-08 13:45:30 -08:00
|
|
|
{
|
2019-03-29 01:34:53 +07:00
|
|
|
(void) rhport;
|
2018-11-08 13:45:30 -08:00
|
|
|
hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex );
|
2018-11-16 21:56:39 +07:00
|
|
|
TU_ASSERT(p_hid);
|
2018-11-08 13:45:30 -08:00
|
|
|
|
2018-11-16 21:56:39 +07:00
|
|
|
if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS &&
|
|
|
|
p_request->bRequest == HID_REQ_CONTROL_SET_REPORT)
|
2018-11-08 13:45:30 -08:00
|
|
|
{
|
2018-11-16 21:56:39 +07:00
|
|
|
// wValue = Report Type | Report ID
|
|
|
|
uint8_t const report_type = tu_u16_high(p_request->wValue);
|
|
|
|
uint8_t const report_id = tu_u16_low(p_request->wValue);
|
2018-11-08 13:45:30 -08:00
|
|
|
|
2019-04-18 00:36:17 +07:00
|
|
|
tud_hid_set_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength);
|
2018-11-08 13:45:30 -08:00
|
|
|
}
|
2018-11-16 21:56:39 +07:00
|
|
|
|
|
|
|
return true;
|
2018-11-08 13:45:30 -08:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:12:06 +07:00
|
|
|
bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
|
2014-03-10 13:13:13 +07:00
|
|
|
{
|
2018-07-23 15:25:45 +07:00
|
|
|
// nothing to do
|
2019-03-29 01:34:53 +07:00
|
|
|
(void) rhport;
|
|
|
|
(void) ep_addr;
|
|
|
|
(void) event;
|
|
|
|
(void) xferred_bytes;
|
|
|
|
|
2018-12-12 13:12:06 +07:00
|
|
|
return true;
|
2014-03-10 13:13:13 +07:00
|
|
|
}
|
|
|
|
|
2018-07-26 16:01:14 +07:00
|
|
|
#endif
|