summaryrefslogtreecommitdiff
path: root/src/class
diff options
context:
space:
mode:
authorhathach <[email protected]>2021-05-13 13:42:52 +0700
committerhathach <[email protected]>2021-05-18 12:58:24 +0700
commit9324fd8f2e549434fd2a399500ae1dc766530046 (patch)
tree9d9762a54150b2e2ddb2d1dd698f522cd71bb2b5 /src/class
parent7305fec4db13e846aa945b26d18418ec1e5e4ce7 (diff)
more hid host API rework
Diffstat (limited to 'src/class')
-rw-r--r--src/class/hid/hid_host.c94
-rw-r--r--src/class/hid/hid_host.h42
2 files changed, 38 insertions, 98 deletions
diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c
index 72cecb8d1..83e65dc52 100644
--- a/src/class/hid/hid_host.c
+++ b/src/class/hid/hid_host.c
@@ -52,19 +52,19 @@ typedef struct
uint8_t report_desc_type;
uint16_t report_desc_len;
-
- bool valid;
- uint16_t report_size; // TODO remove later
+ uint16_t ep_size;
uint8_t boot_protocol; // None, Keyboard, Mouse
bool boot_mode; // Boot or Report protocol
- uint8_t report_count; // Number of reports
+ uint8_t report_count; // Number of reports
struct {
- uint8_t in_len; // length of IN report
- uint8_t out_len; // length of OUT report
uint8_t usage_page;
uint8_t usage;
+
+ // TODO just use the endpint size for now
+ uint8_t in_len; // length of IN report
+ uint8_t out_len; // length of OUT report
}reports[CFG_TUH_HID_REPORT_MAX];
// Parsed Report ID for convenient API
@@ -99,59 +99,43 @@ uint8_t tuh_n_hid_instance_count(uint8_t daddr)
// HID Interface common functions
//--------------------------------------------------------------------+
-// called from public API need to validate parameters
-tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_interface_t *p_hid)
+bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance)
{
- //------------- parameters validation -------------//
- // TODO change to use is configured function
- TU_ASSERT(tuh_device_configured(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
- TU_VERIFY(report, TUSB_ERROR_INVALID_PARA);
- TU_VERIFY(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY);
-
- TU_ASSERT( usbh_edpt_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size) ) ;
-
- return TUSB_ERROR_NONE;
+ hidh_interface_t* hid_itf = get_instance(daddr, instance);
+ return (hid_itf->ep_in != 0) || (hid_itf->ep_out != 0);
}
-//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance)
-//{
-//
-//}
-
-//--------------------------------------------------------------------+
-// KEYBOARD
-//--------------------------------------------------------------------+
-
-bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance)
+bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance)
{
- hidh_interface_t* hid_itf = get_instance(daddr, instance);
+ TU_VERIFY(tuh_n_hid_n_mounted(dev_addr, instance));
- // TODO check rid_keyboard
- return tuh_device_configured(daddr) && (hid_itf->ep_in != 0);
+ hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
+ return !hcd_edpt_busy(dev_addr, hid_itf->ep_in);
}
-bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance)
+bool tuh_n_hid_n_get_report(uint8_t daddr, uint8_t instance, void* report, uint16_t len)
{
+ TU_VERIFY( tuh_device_ready(daddr) && report && len);
hidh_interface_t* hid_itf = get_instance(daddr, instance);
- // TODO check rid_keyboard
- return tuh_device_configured(daddr) && (hid_itf->ep_in != 0);
-}
+ // TODO change to claim endpoint
+ TU_VERIFY( !hcd_edpt_busy(daddr, hid_itf->ep_in) );
-tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* buffer)
-{
- uint8_t inst = 0;
- hidh_interface_t* hid_itf = get_instance(dev_addr, inst);
+ len = tu_min16(len, hid_itf->ep_size);
- return hidh_interface_get_report(dev_addr, buffer, hid_itf);
+ return usbh_edpt_xfer(daddr, hid_itf->ep_in, report, len);
}
-bool tuh_hid_keyboard_is_busy(uint8_t dev_addr)
+//--------------------------------------------------------------------+
+// KEYBOARD
+//--------------------------------------------------------------------+
+
+bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance)
{
- uint8_t inst = 0;
- hidh_interface_t* hid_itf = get_instance(dev_addr, inst);
+ hidh_interface_t* hid_itf = get_instance(daddr, instance);
- return tuh_n_hid_n_keyboard_mounted(dev_addr, inst) && hcd_edpt_busy(dev_addr, hid_itf->ep_in);
+ // TODO check rid_keyboard
+ return tuh_device_ready(daddr) && (hid_itf->ep_in != 0);
}
//--------------------------------------------------------------------+
@@ -165,27 +149,12 @@ static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance)
{
// hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
- return tuh_device_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0);
-}
-
-bool tuh_hid_mouse_is_busy(uint8_t dev_addr)
-{
- return tuh_n_hid_n_mouse_mounted(dev_addr, 0) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in);
-}
-
-tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report)
-{
- return hidh_interface_get_report(dev_addr, report, &mouseh_data[dev_addr-1]);
+ return tuh_device_ready(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0);
}
#endif
//--------------------------------------------------------------------+
-// GENERIC
-//--------------------------------------------------------------------+
-
-
-//--------------------------------------------------------------------+
// USBH API
//--------------------------------------------------------------------+
void hidh_init(void)
@@ -264,10 +233,9 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
hid_dev->inst_count++;
- hid_itf->itf_num = desc_itf->bInterfaceNumber;
- hid_itf->ep_in = desc_ep->bEndpointAddress;
- hid_itf->report_size = desc_ep->wMaxPacketSize.size; // TODO get size from report descriptor
- hid_itf->valid = true;
+ hid_itf->itf_num = desc_itf->bInterfaceNumber;
+ hid_itf->ep_in = desc_ep->bEndpointAddress;
+ hid_itf->ep_size = desc_ep->wMaxPacketSize.size;
// Assume bNumDescriptors = 1
hid_itf->report_desc_type = desc_hid->bReportType;
diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h
index 0052a2b0c..23b23e657 100644
--- a/src/class/hid/hid_host.h
+++ b/src/class/hid/hid_host.h
@@ -61,7 +61,12 @@
uint8_t tuh_n_hid_instance_count(uint8_t daddr);
// Check if HID instance is mounted
-//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance);
+bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance);
+
+// Check if the interface is ready to use
+bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
+
+bool tuh_n_hid_n_get_report(uint8_t daddr, uint8_t instance, void* report, uint16_t len);
// Check if HID instance with Keyboard is mounted
bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance);
@@ -69,6 +74,7 @@ bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance);
// Check if HID instance with Mouse is mounted
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance);
+
//--------------------------------------------------------------------+
// Application API (Single device)
//--------------------------------------------------------------------+
@@ -119,19 +125,6 @@ bool tuh_hid_keyboard_mounted(uint8_t dev_addr);
* \note This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report.
* Alternatively, asynchronous event API can be used
*/
-bool tuh_hid_keyboard_is_busy(uint8_t dev_addr);
-
-/** \brief Perform a get report from Keyboard interface
- * \param[in] dev_addr device address
- * \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
- * \returns \ref tusb_error_t type to indicate success or error condition.
- * \retval TUSB_ERROR_NONE on success
- * \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
- */
-tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void * p_report);
//------------- Application Callback -------------//
/** \brief Callback function that is invoked when an transferring event occurred
@@ -170,27 +163,6 @@ void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr);
* The interface API includes status checking function, data transferring function and callback functions
* @{ */
-/** \brief Check if the interface is currently busy or not
- * \param[in] dev_addr device address
- * \retval true if the interface is busy meaning the stack is still transferring/waiting data from/to device
- * \retval false if the interface is not busy meaning the stack successfully transferred data from/to device
- * \note This function is primarily used for polling/waiting result after \ref tuh_hid_mouse_get_report.
- * Alternatively, asynchronous event API can be used
- */
-bool tuh_hid_mouse_is_busy(uint8_t dev_addr);
-
-/** \brief Perform a get report from Mouse interface
- * \param[in] dev_addr device address
- * \param[in,out] p_report address that is used to store data from device. Must be accessible by usb controller (see \ref CFG_TUSB_MEM_SECTION)
- * \returns \ref tusb_error_t type to indicate success or error condition.
- * \retval TUSB_ERROR_NONE on success
- * \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note This function is non-blocking and returns immediately. The result of usb transfer will be reported by the interface's callback function
- */
-tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void* p_report);
-
//------------- Application Callback -------------//
/** \brief Callback function that is invoked when an transferring event occurred
* \param[in] dev_addr Address of device