2017-04-07 15:40:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Jan Pochyla, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:29:47 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2017-03-26 22:59:52 +00:00
|
|
|
typedef struct __attribute__((packed)) {
|
2017-03-30 14:06:01 +00:00
|
|
|
uint8_t bFunctionLength;
|
|
|
|
uint8_t bDescriptorType;
|
|
|
|
uint8_t bDescriptorSubtype;
|
|
|
|
uint16_t bcdCDC;
|
|
|
|
} usb_vcp_header_descriptor_t;
|
2017-03-26 22:59:52 +00:00
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
2017-03-30 14:06:01 +00:00
|
|
|
uint8_t bFunctionLength;
|
|
|
|
uint8_t bDescriptorType;
|
|
|
|
uint8_t bDescriptorSubtype;
|
|
|
|
uint8_t bmCapabilities;
|
|
|
|
uint8_t bDataInterface;
|
|
|
|
} usb_vcp_cm_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
uint8_t bFunctionLength;
|
|
|
|
uint8_t bDescriptorType;
|
|
|
|
uint8_t bDescriptorSubtype;
|
|
|
|
uint8_t bmCapabilities;
|
|
|
|
} usb_vcp_acm_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
uint8_t bFunctionLength;
|
|
|
|
uint8_t bDescriptorType;
|
|
|
|
uint8_t bDescriptorSubtype;
|
|
|
|
uint8_t bControlInterface;
|
|
|
|
uint8_t bSubordinateInterface0;
|
|
|
|
} usb_vcp_union_descriptor_t;
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
usb_interface_assoc_descriptor_t assoc;
|
|
|
|
usb_interface_descriptor_t iface_cdc;
|
|
|
|
usb_vcp_header_descriptor_t fheader; // Class-Specific Descriptor Header Format
|
|
|
|
usb_vcp_cm_descriptor_t fcm; // Call Management Functional Descriptor
|
|
|
|
usb_vcp_acm_descriptor_t facm; // Abstract Control Management Functional Descriptor
|
|
|
|
usb_vcp_union_descriptor_t funion; // Union Interface Functional Descriptor
|
|
|
|
usb_endpoint_descriptor_t ep_cmd;
|
|
|
|
usb_interface_descriptor_t iface_data;
|
2017-03-26 22:59:52 +00:00
|
|
|
usb_endpoint_descriptor_t ep_in;
|
|
|
|
usb_endpoint_descriptor_t ep_out;
|
|
|
|
} usb_vcp_descriptor_block_t;
|
|
|
|
|
2017-04-07 15:40:22 +00:00
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
uint32_t dwDTERate;
|
|
|
|
uint8_t bCharFormat; // usb_cdc_line_coding_bCharFormat_t
|
|
|
|
uint8_t bParityType; // usb_cdc_line_coding_bParityType_t
|
|
|
|
uint8_t bDataBits;
|
|
|
|
} usb_cdc_line_coding_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
USB_CDC_1_STOP_BITS = 0,
|
|
|
|
USB_CDC_1_5_STOP_BITS = 1,
|
|
|
|
USB_CDC_2_STOP_BITS = 2,
|
|
|
|
} usb_cdc_line_coding_bCharFormat_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
USB_CDC_NO_PARITY = 0,
|
|
|
|
USB_CDC_ODD_PARITY = 1,
|
|
|
|
USB_CDC_EVEN_PARITY = 2,
|
|
|
|
USB_CDC_MARK_PARITY = 3,
|
|
|
|
USB_CDC_SPACE_PARITY = 4,
|
|
|
|
} usb_cdc_line_coding_bParityType_t;
|
|
|
|
|
2017-04-10 16:29:47 +00:00
|
|
|
typedef void (*usb_vcp_intr_fn_t)(void);
|
|
|
|
|
2017-03-26 22:59:52 +00:00
|
|
|
typedef struct {
|
2017-03-30 14:06:01 +00:00
|
|
|
uint8_t iface_num; // Address of this VCP interface
|
|
|
|
uint8_t data_iface_num; // Address of data interface of the VCP interface association
|
|
|
|
uint8_t ep_cmd; // Address of IN CMD endpoint (with the highest bit set)
|
|
|
|
uint8_t ep_in; // Address of IN endpoint (with the highest bit set)
|
|
|
|
uint8_t ep_out; // Address of OUT endpoint
|
|
|
|
uint8_t polling_interval; // In units of 1ms
|
|
|
|
uint8_t max_data_packet_len;
|
2017-04-10 16:29:47 +00:00
|
|
|
|
|
|
|
size_t rx_buffer_len; // Length of rx_buffer, power of 2
|
|
|
|
uint8_t *rx_buffer; // With length of rx_buffer_len bytes
|
|
|
|
uint8_t *rx_packet; // With length of at least max_data_packet_len
|
|
|
|
|
|
|
|
uint8_t rx_intr_val; // Value matched against every received byte
|
|
|
|
usb_vcp_intr_fn_t rx_intr_fn; // Callback called if rx_intr_val matches
|
|
|
|
|
2017-03-26 22:59:52 +00:00
|
|
|
} usb_vcp_info_t;
|
|
|
|
|
2017-04-10 16:29:47 +00:00
|
|
|
typedef struct {
|
|
|
|
size_t cap;
|
|
|
|
volatile size_t read;
|
|
|
|
volatile size_t write;
|
|
|
|
uint8_t *buf;
|
|
|
|
} ring_buffer_t;
|
|
|
|
|
2017-03-26 22:59:52 +00:00
|
|
|
typedef struct {
|
2017-03-30 14:06:01 +00:00
|
|
|
uint8_t is_connected;
|
|
|
|
uint8_t in_idle;
|
|
|
|
|
|
|
|
uint8_t data_iface_num;
|
|
|
|
uint8_t ep_cmd;
|
|
|
|
uint8_t ep_in;
|
|
|
|
uint8_t ep_out;
|
|
|
|
uint8_t polling_interval;
|
|
|
|
uint8_t max_data_packet_len;
|
|
|
|
|
2017-04-10 16:29:47 +00:00
|
|
|
uint8_t *rx_packet;
|
|
|
|
ring_buffer_t rx_ring;
|
|
|
|
|
|
|
|
uint8_t rx_intr_val;
|
|
|
|
usb_vcp_intr_fn_t rx_intr_fn;
|
|
|
|
|
2017-03-26 22:59:52 +00:00
|
|
|
const usb_vcp_descriptor_block_t *desc_block;
|
|
|
|
} usb_vcp_state_t;
|
|
|
|
|
|
|
|
int usb_vcp_add(const usb_vcp_info_t *vcp_info);
|
|
|
|
int usb_vcp_can_read(uint8_t iface_num);
|
|
|
|
int usb_vcp_can_write(uint8_t iface_num);
|
|
|
|
int usb_vcp_read(uint8_t iface_num, uint8_t *buf, uint32_t len);
|
|
|
|
int usb_vcp_write(uint8_t iface_num, const uint8_t *buf, uint32_t len);
|
|
|
|
|
|
|
|
int usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len, uint32_t timeout);
|
|
|
|
int usb_vcp_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len, uint32_t timeout);
|