1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00
LibrePilot/flight/PiOS/inc/pios_usb_defs.h
Stacey Sheldon b94f4b49a4 usb: major refactoring of USB descriptors, CDC and HID
Summary of changes:
 * USB CDC and HID drivers are completely split apart.
   * This will allow different max buffer sizes for HID and CDC.
 * USB descriptors have been overhauled:
   * Proper structs/macros/enums declared for USB (see pios_usb_defs.h)
   * Two common descriptor definitions.  One for HID+CDC another for HID only.
     See pios_usb_desc_{hid_cdc,hid_only}.c for details.
   * Long standing bugs in OP USB descriptors became much more obvious with the
     new struct definitions.
   * Board specific USB initialization is now in pios_usb_board_data.h in each build target.
   * Definition of USB descriptors is now entirely indpendent of STM32 libs.
     Glue into STM32 libs is provided by pios_usbhook.c.
 * Removed a lot of stale/irrelevant USB #defines throughout the tree.
 * Improved naming consistency throughout USB code:
   * PIOS_USB_HID_* now refers to the HID endpoint code.
   * PIOS_USB_CDC_* now refers to the CDC endpoint code.
   * PIOS_USB_* now refers to the low-level USB code.
   * PIOS_USB_BOARD_* now refers to board-specific USB data
   * PIOS_USBHOOK_* is glue between PIOS and STM32 USB libs.
   * struct usb_* and enum usb_* and USB_* and HID_* are all types from the USB spec.
 * Shrunk the buffer size on the CDC call mgmt endpoint to save some RAM.
 * Made a few more USB related variables static to save some RAM.
2012-01-02 14:27:06 -05:00

293 lines
8.6 KiB
C

#ifndef PIOS_USB_DEFS_H
#define PIOS_USB_DEFS_H
#include <stdint.h> /* uint*_t */
enum usb_desc_types {
USB_DESC_TYPE_DEVICE = 0x01,
USB_DESC_TYPE_CONFIGURATION = 0x02,
USB_DESC_TYPE_STRING = 0x03,
USB_DESC_TYPE_INTERFACE = 0x04,
USB_DESC_TYPE_ENDPOINT = 0x05,
USB_DESC_TYPE_IAD = 0x0B,
USB_DESC_TYPE_HID = 0x21,
USB_DESC_TYPE_REPORT = 0x22,
USB_DESC_TYPE_CLASS_SPECIFIC = 0x24,
} __attribute__((packed));
enum usb_interface_class {
USB_INTERFACE_CLASS_CDC = 0x02,
USB_INTERFACE_CLASS_HID = 0x03,
USB_INTERFACE_CLASS_DATA = 0x0A,
} __attribute__((packed));
enum usb_cdc_desc_subtypes {
USB_CDC_DESC_SUBTYPE_HEADER = 0x00,
USB_CDC_DESC_SUBTYPE_CALLMGMT = 0x01,
USB_CDC_DESC_SUBTYPE_ABSTRACT_CTRL = 0x02,
USB_CDC_DESC_SUBTYPE_UNION = 0x06,
} __attribute__((packed));
enum usb_ep_attr {
USB_EP_ATTR_TT_CONTROL = 0x00,
USB_EP_ATTR_TT_ISOCHRONOUS = 0x01,
USB_EP_ATTR_TT_BULK = 0x02,
USB_EP_ATTR_TT_INTERRUPT = 0x03,
} __attribute__((packed));
/* Standard macros to convert from host endian to USB endian (ie. little endian) */
#if __BIG_ENDIAN__
#define htousbs(v) ((uint16_t)(\
((((v) >> 0) & 0xFF) << 8) | \
((((v) >> 8) & 0xFF) << 0)))
#define htousbl(v) ((uint32_t)(\
((((v) >> 0) & 0xFF) << 24) | \
((((v) >> 8) & 0xFF) << 16) | \
((((v) >> 16) & 0xFF) << 8) | \
((((v) >> 24) & 0xFF) << 0)))
#else
#define htousbs(v) (v)
#define htousbl(v) (v)
#endif
#define USB_EP_IN(ep) ((uint8_t) (0x80 | ((ep) & 0xF)))
#define USB_EP_OUT(ep) ((uint8_t) (0x00 | ((ep) & 0xF)))
#define HID_ITEM_TYPE_MAIN 0x0
#define HID_ITEM_TYPE_GLOBAL 0x1
#define HID_ITEM_TYPE_LOCAL 0x2
#define HID_ITEM_TYPE_RSVD 0x3
#define HID_TAG_GLOBAL_USAGE_PAGE 0x0 /* 0b0000 */
#define HID_TAG_GLOBAL_LOGICAL_MIN 0x1 /* 0b0001 */
#define HID_TAG_GLOBAL_LOGICAL_MAX 0x2 /* 0b0010 */
#define HID_TAG_GLOBAL_PHYS_MIN 0x3 /* 0b0011 */
#define HID_TAG_GLOBAL_PHYS_MAX 0x4 /* 0b0100 */
#define HID_TAG_GLOBAL_UNIT_EXP 0x5 /* 0b0101 */
#define HID_TAG_GLOBAL_UNIT 0x6 /* 0b0110 */
#define HID_TAG_GLOBAL_REPORT_SIZE 0x7 /* 0b0111 */
#define HID_TAG_GLOBAL_REPORT_ID 0x8 /* 0b1000 */
#define HID_TAG_GLOBAL_REPORT_CNT 0x9 /* 0b1001 */
#define HID_TAG_GLOBAL_PUSH 0xA /* 0b1010 */
#define HID_TAG_GLOBAL_POP 0xB /* 0b1011 */
#define HID_TAG_MAIN_INPUT 0x8 /* 0b1000 */
#define HID_TAG_MAIN_OUTPUT 0x9 /* 0b1001 */
#define HID_TAG_MAIN_COLLECTION 0xA /* 0b1010 */
#define HID_TAG_MAIN_FEATURE 0xB /* 0b1011 */
#define HID_TAG_MAIN_ENDCOLLECTION 0xC /* 0b1100 */
#define HID_TAG_LOCAL_USAGE 0x0 /* 0b0000 */
#define HID_TAG_LOCAL_USAGE_MIN 0x1 /* 0b0001 */
#define HID_TAG_LOCAL_USAGE_MAX 0x2 /* 0b0010 */
#define HID_TAG_LOCAL_DESIG_INDEX 0x3 /* 0b0011 */
#define HID_TAG_LOCAL_DESIG_MIN 0x4 /* 0b0100 */
#define HID_TAG_LOCAL_DESIG_MAX 0x5 /* 0b0101 */
/* There is no value defined for 0x6 */
#define HID_TAG_LOCAL_STRING_INDEX 0x7 /* 0b0111 */
#define HID_TAG_LOCAL_STRING_MIN 0x8 /* 0b1000 */
#define HID_TAG_LOCAL_STRING_MAX 0x9 /* 0b1001 */
#define HID_TAG_LOCAL_DELIMITER 0xA /* 0b1010 */
#define HID_TAG_RSVD 0xF /* 0b1111 */
#define HID_ITEM_SIZE_0 0
#define HID_ITEM_SIZE_1 1
#define HID_ITEM_SIZE_2 2
#define HID_ITEM_SIZE_4 3 /* Yes, 4 bytes is represented with a size field = 3 */
#define HID_SHORT_ITEM(tag,type,size) (\
(((tag) & 0xF) << 4) | \
(((type) & 0x3) << 2) | \
(((size) & 0x3) << 0))
/* Long items have a fixed prefix */
#define HID_LONG_ITEM HID_SHORT_ITEM(HID_TAG_RSVD, HID_ITEM_TYPE_RSVD, HID_ITEM_SIZE_2)
#define HID_MAIN_ITEM_0(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_MAIN, HID_ITEM_SIZE_0)
#define HID_MAIN_ITEM_1(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_MAIN, HID_ITEM_SIZE_1)
#define HID_MAIN_ITEM_2(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_MAIN, HID_ITEM_SIZE_2)
#define HID_MAIN_ITEM_4(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_MAIN, HID_ITEM_SIZE_4)
#define HID_GLOBAL_ITEM_0(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_GLOBAL, HID_ITEM_SIZE_0)
#define HID_GLOBAL_ITEM_1(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_GLOBAL, HID_ITEM_SIZE_1)
#define HID_GLOBAL_ITEM_2(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_GLOBAL, HID_ITEM_SIZE_2)
#define HID_GLOBAL_ITEM_4(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_GLOBAL, HID_ITEM_SIZE_4)
#define HID_LOCAL_ITEM_0(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_LOCAL, HID_ITEM_SIZE_0)
#define HID_LOCAL_ITEM_1(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_LOCAL, HID_ITEM_SIZE_1)
#define HID_LOCAL_ITEM_2(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_LOCAL, HID_ITEM_SIZE_2)
#define HID_LOCAL_ITEM_3(tag) HID_SHORT_ITEM((tag), HID_ITEM_TYPE_LOCAL, HID_ITEM_SIZE_3)
struct usb_device_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} __attribute__((packed));
struct usb_configuration_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
} __attribute__((packed));
struct usb_interface_association_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bFirstInterface;
uint8_t bInterfaceCount;
uint8_t bFunctionClass;
uint8_t bFunctionSubClass;
uint8_t bFunctionProtocol;
uint8_t iInterface;
} __attribute__((packed));
struct usb_interface_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t nInterfaceProtocol;
uint8_t iInterface;
} __attribute__((packed));
struct usb_hid_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bClassDescriptorType;
uint16_t wItemLength;
} __attribute__((packed));
struct usb_endpoint_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} __attribute__((packed));
struct usb_cdc_header_func_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint16_t bcdCDC;
} __attribute__((packed));
struct usb_cdc_callmgmt_func_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint8_t bmCapabilities;
uint8_t bDataInterface;
} __attribute__((packed));
struct usb_cdc_acm_func_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint8_t bmCapabilities;
} __attribute__((packed));
struct usb_cdc_union_func_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint8_t bMasterInterface;
uint8_t bSlaveInterface;
} __attribute__((packed));
#define USB_LANGID_ENGLISH_UK 0x0809
struct usb_string_langid {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bLangID;
} __attribute__((packed));
struct usb_cdc_line_coding {
uint32_t dwDTERate;
uint8_t bCharFormat;
uint8_t bParityType;
uint8_t bDataBits;
} __attribute__((packed));
enum usb_cdc_line_coding_stop {
USB_CDC_LINE_CODING_STOP_1 = 0,
USB_CDC_LINE_CODING_STOP_1_5 = 1,
USB_CDC_LINE_CODING_STOP_2 = 2,
} __attribute__((packed));
enum usb_cdc_line_coding_parity {
USB_CDC_LINE_CODING_PARITY_NONE = 0,
USB_CDC_LINE_CODING_PARITY_ODD = 1,
USB_CDC_LINE_CODING_PARITY_EVEN = 2,
USB_CDC_LINE_CODING_PARITY_MARK = 3,
USB_CDC_LINE_CODING_PARITY_SPACE = 4,
} __attribute__((packed));
struct usb_cdc_serial_state_report {
uint8_t bmRequestType;
uint8_t bNotification;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint16_t bmUartState;
} __attribute__((packed));
enum usb_cdc_notification {
USB_CDC_NOTIFICATION_SERIAL_STATE = 0x20,
} __attribute__((packed));
/*
* OpenPilot Specific USB Definitions
*/
#define USB_VENDOR_ID_OPENPILOT 0x20A0
enum usb_product_ids {
USB_PRODUCT_ID_OPENPILOT_MAIN = 0x415A,
USB_PRODUCT_ID_COPTERCONTROL = 0x415B,
USB_PRODUCT_ID_PIPXTREME = 0x415C,
} __attribute__((packed));
enum usb_op_board_ids {
USB_OP_BOARD_ID_OPENPILOT_MAIN = 1,
/* Board ID 2 may be unused or AHRS */
USB_OP_BOARD_ID_PIPXTREME = 3,
USB_OP_BOARD_ID_COPTERCONTROL = 4,
} __attribute__((packed));
enum usb_op_board_modes {
USB_OP_BOARD_MODE_BL = 1,
USB_OP_BOARD_MODE_FW = 2,
} __attribute__((packed));
#define USB_OP_DEVICE_VER(board_id, board_mode) (\
((board_id & 0xFF) << 8) | \
((board_mode & 0xFF) << 0))
#endif /* PIOS_USB_DEFS_H */