Clean up usb_descriptor.[ch]

This commit is contained in:
László Monda
2017-04-14 00:03:24 +02:00
parent 7d448058f1
commit 38d07b27ef
2 changed files with 65 additions and 173 deletions
+18 -84
View File
@@ -281,68 +281,43 @@ usb_language_list_t g_language_list = {
usb_language_list_t *g_language_ptr; usb_language_list_t *g_language_ptr;
/* Get hid descriptor request */ usb_status_t usb_device_get_hid_descriptor(
usb_status_t usb_device_get_hid_descriptor(usb_device_handle handle, usb_device_handle handle, usb_device_get_hid_descriptor_struct_t *hid_descriptor)
usb_device_get_hid_descriptor_struct_t *hid_descriptor)
{ {
return kStatus_USB_InvalidRequest; return kStatus_USB_InvalidRequest;
} }
/* Get hid report descriptor request */ /* Get hid report descriptor request */
usb_status_t usb_device_get_hid_report_descriptor(usb_device_handle handle, usb_status_t usb_device_get_hid_report_descriptor(
usb_device_get_hid_report_descriptor_struct_t *hid_report_descriptor) usb_device_handle handle, usb_device_get_hid_report_descriptor_struct_t *hid_report_descriptor)
{ {
if (USB_HID_GENERIC_INTERFACE_INDEX == hid_report_descriptor->interfaceNumber) { if (USB_HID_GENERIC_INTERFACE_INDEX == hid_report_descriptor->interfaceNumber) {
hid_report_descriptor->buffer = g_hid_generic_report_descriptor; hid_report_descriptor->buffer = g_hid_generic_report_descriptor;
// hid_report_descriptor->length = USB_HID_REPORT_DESC_SIZE; hid_report_descriptor->length = USB_HID_REPORT_DESC_SIZE;
hid_report_descriptor->length = sizeof(g_hid_generic_report_descriptor);
} else { } else {
return kStatus_USB_InvalidRequest; return kStatus_USB_InvalidRequest;
} }
return kStatus_USB_Success; return kStatus_USB_Success;
} }
/* Get hid physical descriptor request */
usb_status_t usb_device_get_hid_physical_descriptor( usb_status_t usb_device_get_hid_physical_descriptor(
usb_device_handle handle, usb_device_get_hid_physical_descriptor_struct_t *hid_physical_descriptor) usb_device_handle handle, usb_device_get_hid_physical_descriptor_struct_t *hid_physical_descriptor)
{ {
return kStatus_USB_InvalidRequest; return kStatus_USB_InvalidRequest;
} }
/*! usb_status_t usb_device_get_device_descriptor(
* @brief USB device get device descriptor function. usb_device_handle handle, usb_device_get_device_descriptor_struct_t *device_descriptor)
*
* This function gets the device descriptor of the USB devcie.
*
* @param handle The USB device handle.
* @param device_descriptor The pointer to the device descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t usb_device_get_device_descriptor(usb_device_handle handle,
usb_device_get_device_descriptor_struct_t *device_descriptor)
{ {
device_descriptor->buffer = g_device_descriptor; device_descriptor->buffer = g_device_descriptor;
device_descriptor->length = USB_DEVICE_DESCRIPTOR_LENGTH; device_descriptor->length = USB_DEVICE_DESCRIPTOR_LENGTH;
return kStatus_USB_Success; return kStatus_USB_Success;
} }
/*!
* @brief USB device get configuration descriptor function.
*
* This function gets the configuration descriptor of the USB devcie.
*
* @param handle The USB device handle.
* @param configuration_descriptor The pointer to the configuration descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t usb_device_get_configuration_descriptor( usb_status_t usb_device_get_configuration_descriptor(
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configuration_descriptor) usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configuration_descriptor)
{ {
// if (USB_COMPOSITE_CONFIGURE_INDEX > configuration_descriptor->configuration) if (USB_HID_GENERIC_CONFIGURE_INDEX > configuration_descriptor->configuration) {
if (USB_HID_GENERIC_CONFIGURE_INDEX > configuration_descriptor->configuration)
{
configuration_descriptor->buffer = g_config_descriptor; configuration_descriptor->buffer = g_config_descriptor;
configuration_descriptor->length = USB_CONFIGURE_DESCRIPTOR_LENGTH; configuration_descriptor->length = USB_CONFIGURE_DESCRIPTOR_LENGTH;
return kStatus_USB_Success; return kStatus_USB_Success;
@@ -350,18 +325,8 @@ usb_status_t usb_device_get_configuration_descriptor(
return kStatus_USB_InvalidRequest; return kStatus_USB_InvalidRequest;
} }
/*! usb_status_t usb_device_get_string_descriptor(
* @brief USB device get string descriptor function. usb_device_handle handle, usb_device_get_string_descriptor_struct_t *string_descriptor)
*
* This function gets the string descriptor of the USB devcie.
*
* @param handle The USB device handle.
* @param string_descriptor Pointer to the string descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t usb_device_get_string_descriptor(usb_device_handle handle,
usb_device_get_string_descriptor_struct_t *string_descriptor)
{ {
if (string_descriptor->stringIndex == 0) { if (string_descriptor->stringIndex == 0) {
string_descriptor->buffer = (uint8_t *)g_language_list.languageString; string_descriptor->buffer = (uint8_t *)g_language_list.languageString;
@@ -388,54 +353,23 @@ usb_status_t usb_device_get_string_descriptor(usb_device_handle handle,
return kStatus_USB_Success; return kStatus_USB_Success;
} }
/*!
* @brief USB device set speed function.
*
* This function sets the speed of the USB devcie.
*
* @param handle The USB device handle.
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t usb_device_set_speed(usb_device_handle handle, uint8_t speed) usb_status_t usb_device_set_speed(usb_device_handle handle, uint8_t speed)
{ {
usb_hid_config_descriptor_t *ptr_hid = NULL; usb_hid_config_descriptor_t *ptr_hid = NULL;
ptr_hid = (usb_hid_config_descriptor_t *)&g_config_descriptor[USB_HID_CONFIG_INDEX]; ptr_hid = (usb_hid_config_descriptor_t *)&g_config_descriptor[USB_HID_CONFIG_INDEX];
if (USB_SPEED_HIGH == speed) { // HID interface
// HID interface USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE, ptr_hid->endpoint_in.wMaxPacketSize);
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE, ptr_hid->endpoint_in.bInterval = FS_HID_GENERIC_INTERRUPT_IN_INTERVAL;
ptr_hid->endpoint_in.wMaxPacketSize);
ptr_hid->endpoint_in.bInterval = HS_HID_GENERIC_INTERRUPT_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE, USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE, ptr_hid->endpoint_out.wMaxPacketSize);
ptr_hid->endpoint_out.wMaxPacketSize); ptr_hid->endpoint_out.bInterval = FS_HID_GENERIC_INTERRUPT_OUT_INTERVAL;
ptr_hid->endpoint_out.bInterval = HS_HID_GENERIC_INTERRUPT_OUT_INTERVAL;
} else {
// HID interface
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE,
ptr_hid->endpoint_in.wMaxPacketSize);
ptr_hid->endpoint_in.bInterval = FS_HID_GENERIC_INTERRUPT_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE,
ptr_hid->endpoint_out.wMaxPacketSize);
ptr_hid->endpoint_out.bInterval = FS_HID_GENERIC_INTERRUPT_OUT_INTERVAL;
}
for (uint32_t i = 0; i < USB_HID_GENERIC_ENDPOINT_COUNT; i++) { for (uint32_t i = 0; i < USB_HID_GENERIC_ENDPOINT_COUNT; i++) {
if (USB_SPEED_HIGH == speed) { if (g_hid_generic_endpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) {
if (g_hid_generic_endpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) { g_hid_generic_endpoints[i].maxPacketSize = FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE;
g_hid_generic_endpoints[i].maxPacketSize = HS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE;
} else {
g_hid_generic_endpoints[i].maxPacketSize = HS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE;
}
} else { } else {
if (g_hid_generic_endpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) { g_hid_generic_endpoints[i].maxPacketSize = FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE;
g_hid_generic_endpoints[i].maxPacketSize = FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE;
} else {
g_hid_generic_endpoints[i].maxPacketSize = FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE;
}
} }
} }
+47 -89
View File
@@ -3,97 +3,70 @@
#include "bootloader_common.h" #include "bootloader_common.h"
#include "usb_device_config.h" #include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_class.h"
#include "usb_device_hid.h"
#include "usb_descriptor.h"
#define USB_BCD_VERSION (0x0200) #define USB_BCD_VERSION (0x0200)
/******************************************************************************* #define USB_DEVICE_DESCRIPTOR_LENGTH 18
* For generic HID
*******************************************************************************/
/* usb descritpor length */ #define USB_CONFIGURE_DESCRIPTOR_LENGTH 41
#define USB_DEVICE_DESCRIPTOR_LENGTH (18)
#define USB_CONFIGURE_DESCRIPTOR_LENGTH (41) // 64, HID_only = 41, MSC_ONLY = 32 #define USB_HID_REPORT_DESC_SIZE 76
#define USB_HID_GENERIC_DESCRIPTOR_LENGTH 32
#define USB_CONFIGURE_ONLY_DESCRIPTOR_LENGTH 9
#define USB_INTERFACE_DESCRIPTOR_LENGTH 9
#define USB_HID_DESCRIPTOR_LENGTH 9
#define USB_ENDPOINT_DESCRIPTOR_LENGTH 7
#define USB_MSC_DISK_REPORT_DESCRIPTOR_LENGTH 63
#define USB_IAD_DESC_SIZE 8
#define USB_HID_REPORT_DESC_SIZE (76) #define USB_CONFIGURE_COUNT 1
#define USB_HID_GENERIC_DESCRIPTOR_LENGTH (32) #define USB_STRING_COUNT 4
#define USB_CONFIGURE_ONLY_DESCRIPTOR_LENGTH (9) #define USB_LANGUAGE_COUNT 1
#define USB_INTERFACE_DESCRIPTOR_LENGTH (9)
#define USB_HID_DESCRIPTOR_LENGTH (9)
#define USB_ENDPOINT_DESCRIPTOR_LENGTH (7)
#define USB_MSC_DISK_REPORT_DESCRIPTOR_LENGTH (63)
#define USB_IAD_DESC_SIZE (8)
#define USB_CONFIGURE_COUNT (1)
#define USB_STRING_COUNT (4)
#define USB_LANGUAGE_COUNT (1)
#define USB_HID_CONFIG_INDEX (USB_CONFIGURE_ONLY_DESCRIPTOR_LENGTH) #define USB_HID_CONFIG_INDEX (USB_CONFIGURE_ONLY_DESCRIPTOR_LENGTH)
#define HS_BULK_IN_PACKET_SIZE (512) #define USB_HID_GENERIC_INTERFACE_INDEX 0
#define HS_BULK_OUT_PACKET_SIZE (512)
#define FS_BULK_IN_PACKET_SIZE (64)
#define FS_BULK_OUT_PACKET_SIZE (64)
#define USB_HID_GENERIC_INTERFACE_INDEX (0) #define USB_HID_GENERIC_CONFIGURE_INDEX 1
#define USB_HID_GENERIC_INTERFACE_COUNT 1
// HID #define USB_HID_GENERIC_IN_BUFFER_LENGTH 8
#define USB_HID_GENERIC_CONFIGURE_INDEX (1) #define USB_HID_GENERIC_OUT_BUFFER_LENGTH 8
#define USB_HID_GENERIC_INTERFACE_COUNT (1) #define USB_HID_GENERIC_ENDPOINT_COUNT 2
#define USB_HID_GENERIC_ENDPOINT_IN 1
#define USB_HID_GENERIC_ENDPOINT_OUT 2
#define USB_HID_GENERIC_IN_BUFFER_LENGTH (8) #define USB_HID_GENERIC_CLASS 0x03
#define USB_HID_GENERIC_OUT_BUFFER_LENGTH (8) #define USB_HID_GENERIC_SUBCLASS 0x00
#define USB_HID_GENERIC_ENDPOINT_COUNT (2) #define USB_HID_GENERIC_PROTOCOL 0x00
#define USB_HID_GENERIC_ENDPOINT_IN (1)
#define USB_HID_GENERIC_ENDPOINT_OUT (2)
#define USB_HID_GENERIC_CLASS (0x03) #define FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE 64
#define USB_HID_GENERIC_SUBCLASS (0x00) #define FS_HID_GENERIC_INTERRUPT_OUT_INTERVAL 0x01
#define USB_HID_GENERIC_PROTOCOL (0x00)
#define HS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE (64) #define FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE 64
#define FS_HID_GENERIC_INTERRUPT_OUT_PACKET_SIZE (64) #define FS_HID_GENERIC_INTERRUPT_IN_INTERVAL 0x01
#define HS_HID_GENERIC_INTERRUPT_OUT_INTERVAL (0x04) /* 2^(4-1) = 1ms */
#define FS_HID_GENERIC_INTERRUPT_OUT_INTERVAL (0x01)
#define HS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE (64) #define USB_COMPOSITE_INTERFACE_COUNT USB_HID_GENERIC_INTERFACE_COUNT
#define FS_HID_GENERIC_INTERRUPT_IN_PACKET_SIZE (64)
#define HS_HID_GENERIC_INTERRUPT_IN_INTERVAL (0x04) /* 2^(4-1) = 1ms */
#define FS_HID_GENERIC_INTERRUPT_IN_INTERVAL (0x01)
#define USB_COMPOSITE_INTERFACE_COUNT (USB_HID_GENERIC_INTERFACE_COUNT) #define USB_COMPOSITE_CONFIGURE_INDEX 1
#define USB_COMPOSITE_CONFIGURE_INDEX (1) #define USB_STRING_DESCRIPTOR_HEADER_LENGTH 0x02
#define USB_STRING_DESCRIPTOR_0_LENGTH 0x02
#define USB_STRING_DESCRIPTOR_1_LENGTH 56
#define USB_STRING_DESCRIPTOR_2_LENGTH 32
#define USB_STRING_DESCRIPTOR_3_LENGTH 44
#define USB_STRING_DESCRIPTOR_ERROR_LENGTH 32
#define USB_STRING_DESCRIPTOR_HEADER_LENGTH (0x02) #define USB_CONFIGURE_DRAWN 0x32
#define USB_STRING_DESCRIPTOR_0_LENGTH (0x02)
#define USB_STRING_DESCRIPTOR_1_LENGTH (56)
#define USB_STRING_DESCRIPTOR_2_LENGTH (32)
#define USB_STRING_DESCRIPTOR_3_LENGTH (44)
#define USB_STRING_DESCRIPTOR_ERROR_LENGTH (32)
#define USB_CONFIGURE_DRAWN (0x32) enum _usb_descriptor_index {
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
enum _usb_descriptor_index
{
kUsbDescriptorIndex_VidLow = 8, kUsbDescriptorIndex_VidLow = 8,
kUsbDescriptorIndex_VidHigh = 9, kUsbDescriptorIndex_VidHigh = 9,
kUsbDescriptorIndex_PidLow = 10, kUsbDescriptorIndex_PidLow = 10,
kUsbDescriptorIndex_PidHigh = 11 kUsbDescriptorIndex_PidHigh = 11
}; };
typedef struct _usb_hid_config_descriptor typedef struct _usb_hid_config_descriptor {
{
usb_descriptor_interface_t interface; /* Interface descriptor */ usb_descriptor_interface_t interface; /* Interface descriptor */
usb_descriptor_interface_t hid_report; /* Interface descriptor */ usb_descriptor_interface_t hid_report; /* Interface descriptor */
usb_descriptor_endpoint_t endpoint_in; /* Endpoint descriptor */ usb_descriptor_endpoint_t endpoint_in; /* Endpoint descriptor */
@@ -101,38 +74,23 @@ typedef struct _usb_hid_config_descriptor
} usb_hid_config_descriptor_t; } usb_hid_config_descriptor_t;
extern usb_device_class_struct_t g_hid_generic_class; extern usb_device_class_struct_t g_hid_generic_class;
extern usb_device_class_struct_t g_msc_class;
/* Configure the device according to the USB speed. */
extern usb_status_t usb_device_set_speed(usb_device_handle handle, uint8_t speed); extern usb_status_t usb_device_set_speed(usb_device_handle handle, uint8_t speed);
/* Get device descriptor request */ usb_status_t usb_device_get_device_descriptor(
usb_status_t usb_device_get_device_descriptor(usb_device_handle handle, usb_device_handle handle, usb_device_get_device_descriptor_struct_t *device_descriptor);
usb_device_get_device_descriptor_struct_t *device_descriptor);
/* Get device configuration descriptor request */
usb_status_t usb_device_get_configuration_descriptor( usb_status_t usb_device_get_configuration_descriptor(
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configuration_descriptor); usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configuration_descriptor);
usb_status_t usb_device_get_string_descriptor(
/* Get device string descriptor request */ usb_device_handle handle, usb_device_get_string_descriptor_struct_t *string_descriptor);
usb_status_t usb_device_get_string_descriptor(usb_device_handle handle, usb_status_t usb_device_get_hid_descriptor(
usb_device_get_string_descriptor_struct_t *string_descriptor); usb_device_handle handle, usb_device_get_hid_descriptor_struct_t *hid_descriptor);
usb_status_t usb_device_get_hid_report_descriptor(
/* Get hid descriptor request */ usb_device_handle handle, usb_device_get_hid_report_descriptor_struct_t *hid_report_descriptor);
usb_status_t usb_device_get_hid_descriptor(usb_device_handle handle,
usb_device_get_hid_descriptor_struct_t *hid_descriptor);
/* Get hid report descriptor request */
usb_status_t usb_device_get_hid_report_descriptor(usb_device_handle handle,
usb_device_get_hid_report_descriptor_struct_t *hid_report_descriptor);
/* Get hid physical descriptor request */
usb_status_t usb_device_get_hid_physical_descriptor( usb_status_t usb_device_get_hid_physical_descriptor(
usb_device_handle handle, usb_device_get_hid_physical_descriptor_struct_t *hid_physical_descriptor); usb_device_handle handle, usb_device_get_hid_physical_descriptor_struct_t *hid_physical_descriptor);
extern uint8_t g_device_descriptor[]; extern uint8_t g_device_descriptor[];
extern usb_language_list_t g_language_list; extern usb_language_list_t g_language_list;
extern usb_language_list_t *g_language_ptr; extern usb_language_list_t *g_language_ptr;
#endif #endif