From db9d97c9473f29ad59a87d641e6d41a27a00f332 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 9 Mar 2022 11:03:29 +0700 Subject: [PATCH] add tuh_descriptor_hid_report_get() --- examples/host/bare_api/src/main.c | 48 ++++++++++++++++++++++++++----- src/class/hid/hid_host.c | 17 +---------- src/host/usbh.c | 22 ++++++++++++++ src/host/usbh.h | 3 ++ 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/examples/host/bare_api/src/main.c b/examples/host/bare_api/src/main.c index b6b1527c0..2fe5999bc 100644 --- a/examples/host/bare_api/src/main.c +++ b/examples/host/bare_api/src/main.c @@ -64,23 +64,57 @@ int main(void) // TinyUSB Callbacks //--------------------------------------------------------------------+ -void print_device_descriptor(uint8_t dev_addr) +uint8_t usb_buf[256] TU_ATTR_ALIGNED(4); + +tusb_desc_device_t desc_device; + +bool print_device_descriptor(uint8_t daddr, tusb_control_request_t const * request, xfer_result_t result) { - (void) dev_addr; + (void) request; + + if ( XFER_RESULT_SUCCESS != result ) + { + printf("Failed to get device descriptor\r\n"); + return false; + } + + printf("Rhport %u Device %u: ID %04x:%04x\r\n", 0, daddr, desc_device.idVendor, desc_device.idProduct); printf("Device Descriptor:\r\n"); + printf(" bLength %u\r\n", desc_device.bLength); + printf(" bDescriptorType %u\r\n", desc_device.bDescriptorType); + printf(" bcdUSB %04x\r\n", desc_device.bcdUSB); + + printf(" bDeviceClass %u\r\n", desc_device.bDeviceClass); + printf(" bDeviceSubClass %u\r\n", desc_device.bDeviceSubClass); + printf(" bDeviceProtocol %u\r\n", desc_device.bDeviceProtocol); + printf(" bMaxPacketSize0 %u\r\n", desc_device.bMaxPacketSize0); + + printf(" idVendor 0x%04x\r\n", desc_device.idVendor); + printf(" idProduct 0x%04x\r\n", desc_device.idProduct); + printf(" bcdDevice %04x\r\n", desc_device.bcdDevice); + + printf(" iManufacturer %u\r\n", desc_device.iManufacturer); + printf(" iProduct %u\r\n", desc_device.iProduct); + printf(" iSerialNumber %u\r\n", desc_device.iSerialNumber); + + printf(" bNumConfigurations %u\r\n", desc_device.bNumConfigurations); + + return true; } // Invoked when device is mounted (configured) -void tuh_mount_cb (uint8_t dev_addr) +void tuh_mount_cb (uint8_t daddr) { - printf("Device attached, address = %d\r\n", dev_addr); - print_device_descriptor(dev_addr); + printf("Device attached, address = %d\r\n", daddr); + + // get device descriptor + tuh_descriptor_device_get(daddr, &desc_device, 18, print_device_descriptor); } /// Invoked when device is unmounted (bus reset/unplugged) -void tuh_umount_cb(uint8_t dev_addr) +void tuh_umount_cb(uint8_t daddr) { - printf("Device removed, address = %d\r\n", dev_addr); + printf("Device removed, address = %d\r\n", daddr); } //--------------------------------------------------------------------+ diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 9190642c6..32bb775b0 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -409,22 +409,7 @@ static bool config_get_report_desc(uint8_t dev_addr, tusb_control_request_t cons config_driver_mount_complete(dev_addr, instance, NULL, 0); }else { - TU_LOG2("HID Get Report Descriptor\r\n"); - tusb_control_request_t const new_request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_STANDARD, - .direction = TUSB_DIR_IN - }, - .bRequest = TUSB_REQ_GET_DESCRIPTOR, - .wValue = tu_u16(hid_itf->report_desc_type, 0), - .wIndex = itf_num, - .wLength = hid_itf->report_desc_len - }; - - TU_ASSERT(tuh_control_xfer(dev_addr, &new_request, usbh_get_enum_buf(), config_get_report_desc_complete)); + TU_ASSERT(tuh_descriptor_hid_report_get(dev_addr, itf_num, hid_itf->report_desc_type, usbh_get_enum_buf(), hid_itf->report_desc_len, config_get_report_desc_complete)); } return true; diff --git a/src/host/usbh.c b/src/host/usbh.c index 192979ce6..b047179c5 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -299,6 +299,7 @@ bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer bool tuh_descriptor_device_get(uint8_t daddr, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb) { + len = tu_min16(len, sizeof(tusb_desc_device_t)); return tuh_descriptor_get(daddr, TUSB_DESC_DEVICE, 0, buffer, len, complete_cb); } @@ -361,6 +362,27 @@ bool tuh_descriptor_string_serial_get(uint8_t daddr, uint16_t language_id, void* return tuh_descriptor_string_get(daddr, language_id, dev->i_serial, buffer, len, complete_cb); } +// Get HID report descriptor +bool tuh_descriptor_hid_report_get(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb) +{ + TU_LOG2("HID Get Report Descriptor\r\n"); + tusb_control_request_t const request = + { + .bmRequestType_bit = + { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_STANDARD, + .direction = TUSB_DIR_IN + }, + .bRequest = TUSB_REQ_GET_DESCRIPTOR, + .wValue = tu_htole16(TU_U16(desc_type, 0)), + .wIndex = itf_num, + .wLength = len + }; + + return tuh_control_xfer(daddr, &request, buffer, complete_cb); +} + bool tuh_configuration_set(uint8_t daddr, uint8_t config_num, tuh_control_complete_cb_t complete_cb) { TU_LOG2("Set Configuration = %d\r\n", config_num); diff --git a/src/host/usbh.h b/src/host/usbh.h index 9465d1d1b..6fea2fac5 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -112,6 +112,9 @@ bool tuh_descriptor_string_product_get(uint8_t daddr, uint16_t language_id, void // Get serial string descriptor bool tuh_descriptor_string_serial_get(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb); +// Get HID report descriptor +bool tuh_descriptor_hid_report_get(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb); + //--------------------------------------------------------------------+ // APPLICATION CALLBACK //--------------------------------------------------------------------+