mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-04 21:58:22 +00:00
BLE radio support
This commit is contained in:
1 parent
a8d6573ccc
commit
0582cd6724
81 files changed
+7053
-4906
No files matched your search
+333
-127
@@ -1,53 +1,65 @@
|
||||
#include "drivers/usb.h"
|
||||
#include "net_builtin.h"
|
||||
#include "drivers/usb.h"
|
||||
#include "log.h"
|
||||
#include "printf.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_USB
|
||||
// USB Ethernet needs the TCP/IP stack, the serial console does not
|
||||
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||
(MG_ENABLE_DRIVER_USB_RP || MG_ENABLE_DRIVER_USB_NRF52 || \
|
||||
MG_ENABLE_DRIVER_USB_DWC2) && \
|
||||
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||
|
||||
// Interfaces: NCM control and data 0/1 if enabled, then ACM control and data
|
||||
#define MG_USB_SERIAL_IF (2 * MG_ENABLE_USB_ETHERNET)
|
||||
#define MG_USB_NUM_IFS (2 * MG_ENABLE_USB_ETHERNET + 2 * MG_ENABLE_USB_SERIAL)
|
||||
#define MG_USB_CONFIG_LEN \
|
||||
// Interfaces: ACM control and data 0/1 if enabled, then NCM control and data.
|
||||
// The serial port comes first, so that its name on macOS does not depend on
|
||||
// Ethernet: /dev/cu.usbmodem...01
|
||||
#define MGI_USB_ETH_IF (MG_USB_IF_ETH_DATA - 1)
|
||||
#define MGI_USB_NUM_IFS (2 * MG_ENABLE_USB_ETHERNET + 2 * MG_ENABLE_USB_SERIAL)
|
||||
#define MGI_USB_CONFIG_LEN \
|
||||
(9 + 85 * MG_ENABLE_USB_ETHERNET + 66 * MG_ENABLE_USB_SERIAL)
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
#define MG_USB_PRODUCT "MongooseUsbEthernet"
|
||||
#define MGI_USB_PRODUCT "MongooseUsbEthernet"
|
||||
#else
|
||||
#define MG_USB_PRODUCT "MongooseUsbSerial"
|
||||
#define MGI_USB_PRODUCT "MongooseUsbSerial"
|
||||
#endif
|
||||
|
||||
// 0xdead is our example VID, for development only
|
||||
// VID 0xadde, PID 0x40e4: an example, for development only
|
||||
static const uint8_t s_device_desc[] = {
|
||||
18, 1, 1, 2, 0xef, 2, 1, 64, 0xde, 0xad, 0xe4, 0x40, 0, 1, 1, 2, 3, 1};
|
||||
|
||||
// NCM: endpoints 0x81, 0x02/0x82. ACM: endpoints 0x83, 0x04/0x84
|
||||
// clang-format off
|
||||
static const uint8_t s_config_desc[] = {
|
||||
9, 2, MG_USB_CONFIG_LEN, 0, MG_USB_NUM_IFS, 1, 0, 0x80, 50, // Config
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
8, 11, 0, 2, 2, 13, 0, 4, // NCM association
|
||||
9, 4, 0, 0, 1, 2, 13, 0, 4, // NCM control interface
|
||||
5, 0x24, 0, 0x20, 1, // CDC header
|
||||
5, 0x24, 6, 0, 1, // Union
|
||||
13, 0x24, 15, 5, 0, 0, 0, 0, 0xea,
|
||||
5, 0, 0, 0, // Eth, 1514 bytes
|
||||
6, 0x24, 0x1a, 0, 1, 0x21, // NCM 1.0
|
||||
7, 5, 0x81, 3, 16, 0, 16, // Notification endpoint
|
||||
9, 4, 1, 0, 0, 10, 0, 1, 0, // Data interface, off
|
||||
9, 4, 1, 1, 2, 10, 0, 1, 0, // Data interface, on
|
||||
7, 5, 0x02, 2, 64, 0, 0, // Data OUT
|
||||
7, 5, 0x82, 2, 64, 0, 0, // Data IN
|
||||
#endif
|
||||
9, 2, MGI_USB_CONFIG_LEN, 0, MGI_USB_NUM_IFS, 1, 0, 0x80,
|
||||
50, // Config
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
8, 11, MG_USB_SERIAL_IF, 2, 2, 2, 1, 6, // ACM association
|
||||
9, 4, MG_USB_SERIAL_IF, 0, 1, 2, 2, 1, 6, // ACM control interface
|
||||
5, 0x24, 0, 0x20, 1, // CDC header
|
||||
5, 0x24, 1, 0, MG_USB_SERIAL_IF + 1, // Call management
|
||||
4, 0x24, 2, 2, // ACM
|
||||
5, 0x24, 6, MG_USB_SERIAL_IF, MG_USB_SERIAL_IF + 1, // Union
|
||||
7, 5, 0x83, 3, 16, 0, 16, // Notification endpoint
|
||||
9, 4, MG_USB_SERIAL_IF + 1, 0, 2, 10, 0, 0, 0, // Data interface
|
||||
7, 5, 0x04, 2, 64, 0, 0, // Data OUT
|
||||
7, 5, 0x84, 2, 64, 0, 0, // Data IN
|
||||
8, 11, 0, 2, 2, 2, 1, 6, // ACM association
|
||||
9, 4, 0, 0, 1, 2, 2, 1, 6, // ACM control interface
|
||||
5, 0x24, 0, 0x20, 1, // CDC header
|
||||
5, 0x24, 1, 0, 1, // Call management
|
||||
4, 0x24, 2, 2, // ACM
|
||||
5, 0x24, 6, 0, 1, // Union
|
||||
7, 5, 0x83, 3, 16, 0, 16, // Notification endpoint
|
||||
9, 4, 1, 0, 2, 10, 0, 0, 0, // Data interface
|
||||
7, 5, 0x04, 2, 64, 0, 0, // Data OUT
|
||||
7, 5, 0x84, 2, 64, 0, 0, // Data IN
|
||||
#endif
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
8, 11, MGI_USB_ETH_IF, 2, 2, 13, 0, 4, // NCM association
|
||||
9, 4, MGI_USB_ETH_IF, 0, 1, 2, 13, 0, 4, // NCM control interface
|
||||
5, 0x24, 0, 0x20, 1, // CDC header
|
||||
5, 0x24, 6, MGI_USB_ETH_IF, MGI_USB_ETH_IF + 1, // Union
|
||||
13, 0x24, 15, 5, 0, 0, 0, 0, 0xea,
|
||||
5, 0, 0, 0, // Eth, 1514 bytes
|
||||
6, 0x24, 0x1a, 0, 1, 0x21, // NCM 1.0
|
||||
7, 5, 0x81, 3, 16, 0, 16, // Notification endpoint
|
||||
9, 4, MGI_USB_ETH_IF + 1, 0, 0, 10, 0, 1, 0, // Data interface, off
|
||||
9, 4, MGI_USB_ETH_IF + 1, 1, 2, 10, 0, 1, 0, // Data interface, on
|
||||
7, 5, 0x02, 2, 64, 0, 0, // Data OUT
|
||||
7, 5, 0x82, 2, 64, 0, 0, // Data IN
|
||||
#endif
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
// Endpoint addresses, for CLEAR_FEATURE(ENDPOINT_HALT)
|
||||
static const uint8_t s_endpoints[] = {
|
||||
@@ -61,12 +73,14 @@ static const uint8_t s_endpoints[] = {
|
||||
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
// Windows 11 binds its NCM driver through a Microsoft OS 2.0 compatible ID
|
||||
// clang-format off
|
||||
static const uint8_t s_ms_os_desc[] = {
|
||||
10, 0, 0, 0, 0, 0, 3, 6, 46, 0, // Set header, Windows 8.1+
|
||||
8, 0, 1, 0, 0, 0, 36, 0, // Configuration subset
|
||||
8, 0, 2, 0, 0, 0, 28, 0, // Function subset, NCM
|
||||
8, 0, 2, 0, MGI_USB_ETH_IF, 0, 28, 0, // Function subset, NCM
|
||||
20, 0, 3, 0, 'W', 'I', 'N', 'N', 'C', 'M', // Compatible ID
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
// clang-format on
|
||||
static const uint8_t s_bos_desc[] = {
|
||||
5, 15, 33, 0, 1, 28, 16, 5, 0, 0xdf,
|
||||
0x60, 0xdd, 0xd8, 0x89, 0x45, 0xc7, 0x4c, 0x9c, 0xd2, 0x65,
|
||||
@@ -93,51 +107,122 @@ static const uint8_t s_nth[30] = {'N', 'C', 'M', 'H', 12, 0, 0, 0, 0, 0,
|
||||
30, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
#endif
|
||||
|
||||
// Build string descriptor i in u->buf. Returns its length, 0 if none
|
||||
static int string_desc(struct mg_usb *u, unsigned i) {
|
||||
static const char *strings[] = {
|
||||
NULL, "Mongoose", MG_USB_PRODUCT, NULL,
|
||||
"USB Ethernet", NULL, "USB Serial"};
|
||||
const char *s = i < 7 ? strings[i] : NULL;
|
||||
// An IN transfer on an endpoint, sent a packet at a time
|
||||
struct mgi_usb_in {
|
||||
const uint8_t *data; // Bytes still to send
|
||||
size_t left; // How many
|
||||
unsigned packets; // Packets still to send: a short or empty one ends it
|
||||
bool busy; // A packet is waiting for the host
|
||||
};
|
||||
|
||||
// The USB device. There is one
|
||||
static struct mgi_usb {
|
||||
const struct mg_usb_hw *hw; // Controller, NULL until mg_usb_init()
|
||||
struct mgi_usb_in in[5]; // IN transfers, by endpoint
|
||||
char serial[17]; // Serial number, from the controller
|
||||
bool configured; // Host has set the configuration
|
||||
uint8_t buf[66]; // Generated control replies
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
char mac[13];
|
||||
struct mg_tcpip_if *ifp; // Ethernet interface, NULL: serial only
|
||||
bool alt; // Ethernet open: data interface setting 1
|
||||
uint16_t filter, sequence; // Host packet filter, next IN block number
|
||||
uint32_t input_size; // Largest IN block the host accepts
|
||||
uint8_t notify; // Pending link notifications
|
||||
struct mg_queue txq; // Transfer blocks for the host, see tx
|
||||
char txq_buf[MG_USB_TX_SIZE]; // Its storage
|
||||
uint8_t rx[MG_USB_NTB_SIZE]; // OUT transfer block being received
|
||||
size_t rxlen; // How much of it arrived
|
||||
bool discard; // It is too long: drop it
|
||||
#endif
|
||||
size_t k, n;
|
||||
if (i == 0) return u->buf[0] = 4, u->buf[1] = 3, u->buf[2] = 9, u->buf[3] = 4;
|
||||
if (i == 3) s = u->serial;
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
bool dtr; // Terminal open
|
||||
uint8_t line[7]; // Serial line coding
|
||||
char log[MG_USB_LOG_SIZE]; // Log ring
|
||||
unsigned head, tail; // Log ring write and read counters
|
||||
char out[64]; // Log output being sent
|
||||
#endif
|
||||
} s_usb;
|
||||
|
||||
// Send the next packet of the IN transfer on ep. False if it is over
|
||||
static bool mgi_usb_in_next(unsigned ep) {
|
||||
struct mgi_usb_in *e = &s_usb.in[ep];
|
||||
size_t n = e->left > 64 ? 64 : e->left;
|
||||
if (e->packets == 0) return false;
|
||||
s_usb.hw->in((enum mg_usb_ep) ep, e->data, n);
|
||||
e->data += n, e->left -= n, e->packets--;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Start an IN transfer. zlp: end a multiple of 64 bytes with an empty packet
|
||||
static void mgi_usb_in_start(unsigned ep, const void *data, size_t len,
|
||||
bool zlp) {
|
||||
struct mgi_usb_in *e = &s_usb.in[ep];
|
||||
e->data = (const uint8_t *) data, e->left = len;
|
||||
e->packets = (unsigned) (len / 64 + (len % 64 || len == 0 || zlp));
|
||||
e->busy = mgi_usb_in_next(ep);
|
||||
}
|
||||
|
||||
// Cancel the transfers on ep
|
||||
static void mgi_usb_idle(unsigned ep) {
|
||||
memset(&s_usb.in[ep], 0, sizeof(s_usb.in[ep]));
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (i == 5) { // Host end of the link: the device MAC, first byte changed
|
||||
uint8_t *m = u->ifp->mac;
|
||||
mg_snprintf(mac, sizeof(mac), "%02X%02X%02X%02X%02X%02X",
|
||||
if (ep == MG_USB_EP_ETH_DATA) s_usb.rxlen = 0, s_usb.discard = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Unconfigured, on start, bus reset and SET_CONFIGURATION. Keeps the log
|
||||
static void mgi_usb_unconfigure(void) {
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
static const uint8_t line[7] = {0, 0xc2, 1, 0, 0, 0, 8}; // 115200 8N1
|
||||
s_usb.dtr = false;
|
||||
memcpy(s_usb.line, line, sizeof(line));
|
||||
#endif
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
s_usb.alt = false, s_usb.filter = s_usb.sequence = 0, s_usb.notify = 0;
|
||||
s_usb.input_size = MG_USB_NTB_SIZE;
|
||||
#endif
|
||||
s_usb.configured = false;
|
||||
}
|
||||
|
||||
// Build string descriptor i in s_usb.buf. Returns its length, 0 if none
|
||||
static int mgi_usb_string(unsigned i) {
|
||||
static const char *strings[] = {NULL, "Mongoose", MGI_USB_PRODUCT,
|
||||
NULL, "USB Ethernet", NULL,
|
||||
"USB Serial"};
|
||||
const char *s = i < 7 ? strings[i] : NULL;
|
||||
char tmp[33]; // Built strings: product name, host MAC
|
||||
uint8_t *b = s_usb.buf;
|
||||
size_t k, n;
|
||||
if (i == 0) return b[0] = 4, b[1] = 3, b[2] = 9, b[3] = 4;
|
||||
if (i == 2) { // Serial's last 4 digits: tell devices apart
|
||||
size_t len = strlen(s_usb.serial);
|
||||
mg_snprintf(tmp, sizeof(tmp), "%s_%s", MGI_USB_PRODUCT,
|
||||
s_usb.serial + (len > 4 ? len - 4 : 0));
|
||||
s = tmp;
|
||||
}
|
||||
if (i == 3) s = s_usb.serial;
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (i == 5 && s_usb.ifp != NULL) { // Host end: our MAC, first byte changed
|
||||
uint8_t *m = s_usb.ifp->mac;
|
||||
mg_snprintf(tmp, sizeof(tmp), "%02X%02X%02X%02X%02X%02X",
|
||||
((m[0] ^ 4) | 2) & ~1, m[1], m[2], m[3], m[4], m[5]);
|
||||
s = mac;
|
||||
s = tmp;
|
||||
}
|
||||
#endif
|
||||
if (s == NULL) return 0;
|
||||
n = strlen(s) > 32 ? 32 : strlen(s);
|
||||
memset(u->buf, 0, sizeof(u->buf));
|
||||
for (k = 0; k < n; k++) u->buf[2 + k * 2] = (uint8_t) s[k];
|
||||
u->buf[0] = (uint8_t) (2 + n * 2), u->buf[1] = 3;
|
||||
return u->buf[0];
|
||||
memset(b, 0, sizeof(s_usb.buf));
|
||||
for (k = 0; k < n; k++) b[2 + k * 2] = (uint8_t) s[k];
|
||||
b[0] = (uint8_t) (2 + n * 2), b[1] = 3;
|
||||
return b[0];
|
||||
}
|
||||
|
||||
void mg_usb_reset(struct mg_usb *u) {
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
static const uint8_t line[7] = {0, 0xc2, 1, 0, 0, 0, 8}; // 115200 8N1
|
||||
u->dtr = false;
|
||||
memcpy(u->line, line, sizeof(line));
|
||||
#endif
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
u->alt = false, u->filter = u->sequence = 0, u->notify = 0;
|
||||
u->input_size = MG_USB_NTB_SIZE;
|
||||
#endif
|
||||
u->configured = false;
|
||||
}
|
||||
|
||||
int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
// Answer a control request r, its OUT data at r + 8. Returns -1 to stall,
|
||||
// else the IN reply length in *reply, 0 for status only
|
||||
static int mgi_usb_request(const uint8_t *r, const void **reply) {
|
||||
unsigned value = MG_LOAD_LE16(r + 2), index = MG_LOAD_LE16(r + 4);
|
||||
unsigned wlen = MG_LOAD_LE16(r + 6);
|
||||
const void *d = u->buf;
|
||||
unsigned wlen = MG_LOAD_LE16(r + 6), i;
|
||||
const void *d = s_usb.buf;
|
||||
int n = 0;
|
||||
switch ((r[0] << 8) | r[1]) {
|
||||
case 0x8006: // GET_DESCRIPTOR
|
||||
@@ -146,32 +231,37 @@ int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (value >> 8 == 15) d = s_bos_desc, n = sizeof(s_bos_desc);
|
||||
#endif
|
||||
if (value >> 8 == 3) n = string_desc(u, value & 255);
|
||||
if (value >> 8 == 3) n = mgi_usb_string(value & 255);
|
||||
if (n == 0) return -1;
|
||||
break;
|
||||
case 0x0005: // SET_ADDRESS, applied by the driver after the status
|
||||
case 0x0005: // SET_ADDRESS, applied by the controller
|
||||
if (value > 127) return -1;
|
||||
break;
|
||||
case 0x0009: // SET_CONFIGURATION
|
||||
if (value > 1) return -1;
|
||||
mg_usb_reset(u);
|
||||
u->configured = value == 1;
|
||||
mgi_usb_unconfigure();
|
||||
for (i = 1; i < 5; i++) mgi_usb_idle(i);
|
||||
s_usb.configured = value == 1;
|
||||
break;
|
||||
case 0x8008: // GET_CONFIGURATION
|
||||
u->buf[0] = u->configured, n = 1;
|
||||
s_usb.buf[0] = s_usb.configured, n = 1;
|
||||
break;
|
||||
case 0x010b: // SET_INTERFACE: only the Ethernet data interface has 1
|
||||
if (index >= MG_USB_NUM_IFS ||
|
||||
value > (unsigned) (MG_ENABLE_USB_ETHERNET && index == 1))
|
||||
if (index >= MGI_USB_NUM_IFS ||
|
||||
value > (unsigned) (MG_ENABLE_USB_ETHERNET &&
|
||||
index == MG_USB_IF_ETH_DATA))
|
||||
return -1;
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (index == 1) u->alt = value == 1, u->notify = 1;
|
||||
if (index == MG_USB_IF_ETH_DATA) {
|
||||
s_usb.alt = value == 1, s_usb.notify = 1;
|
||||
mgi_usb_idle(MG_USB_EP_ETH_DATA);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case 0x810a: // GET_INTERFACE
|
||||
u->buf[0] = 0, n = 1;
|
||||
s_usb.buf[0] = 0, n = 1;
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (index == 1) u->buf[0] = u->alt;
|
||||
if (index == MG_USB_IF_ETH_DATA) s_usb.buf[0] = s_usb.alt;
|
||||
#endif
|
||||
break;
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
@@ -180,9 +270,9 @@ int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
case 0x8000: // GET_STATUS: device, interface, endpoint
|
||||
case 0x8100:
|
||||
case 0x8200:
|
||||
u->buf[0] = u->buf[1] = 0, n = 2;
|
||||
s_usb.buf[0] = s_usb.buf[1] = 0, n = 2;
|
||||
break;
|
||||
case 0x0201: // CLEAR_FEATURE(ENDPOINT_HALT), applied by the driver
|
||||
case 0x0201: // CLEAR_FEATURE(ENDPOINT_HALT), applied by the controller
|
||||
if (value || index > 255 ||
|
||||
!memchr(s_endpoints, (int) index, sizeof(s_endpoints)))
|
||||
return -1;
|
||||
@@ -193,7 +283,7 @@ int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
d = s_ms_os_desc, n = sizeof(s_ms_os_desc);
|
||||
break;
|
||||
case 0x2143: // SET_ETHERNET_PACKET_FILTER
|
||||
u->filter = (uint16_t) value;
|
||||
s_usb.filter = (uint16_t) value;
|
||||
break;
|
||||
case 0xa180: // GET_NTB_PARAMETERS
|
||||
d = s_ntb_params, n = sizeof(s_ntb_params);
|
||||
@@ -202,30 +292,29 @@ int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
if (value) return -1;
|
||||
break;
|
||||
case 0xa185: // GET_NTB_INPUT_SIZE, one datagram per block
|
||||
memset(u->buf, 0, 8);
|
||||
MG_STORE_LE32(u->buf, u->input_size);
|
||||
u->buf[4] = 1, n = 8;
|
||||
memset(s_usb.buf, 0, 8);
|
||||
MG_STORE_LE32(s_usb.buf, s_usb.input_size);
|
||||
s_usb.buf[4] = 1, n = 8;
|
||||
break;
|
||||
case 0x2186: // SET_NTB_INPUT_SIZE
|
||||
if ((wlen != 4 && wlen != 8) || MG_LOAD_LE32(r + 8) < 2048 ||
|
||||
MG_LOAD_LE32(r + 8) > 65535)
|
||||
return -1;
|
||||
u->input_size = MG_LOAD_LE32(r + 8);
|
||||
if (u->input_size > MG_USB_NTB_SIZE) u->input_size = MG_USB_NTB_SIZE;
|
||||
s_usb.input_size = MG_LOAD_LE32(r + 8);
|
||||
if (s_usb.input_size > MG_USB_NTB_SIZE)
|
||||
s_usb.input_size = MG_USB_NTB_SIZE;
|
||||
break;
|
||||
#endif
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
case 0xa121: // GET_LINE_CODING
|
||||
d = u->line, n = sizeof(u->line);
|
||||
d = s_usb.line, n = sizeof(s_usb.line);
|
||||
break;
|
||||
case 0x2120: // SET_LINE_CODING
|
||||
if (wlen != sizeof(u->line)) return -1;
|
||||
memcpy(u->line, r + 8, sizeof(u->line));
|
||||
if (wlen != sizeof(s_usb.line)) return -1;
|
||||
memcpy(s_usb.line, r + 8, sizeof(s_usb.line));
|
||||
break;
|
||||
case 0x2122: // SET_CONTROL_LINE_STATE. Closed at 1200 baud: bootloader,
|
||||
u->dtr = value & 1; // after a delay that lets this request complete
|
||||
if (!u->dtr && MG_LOAD_LE32(u->line) == 1200)
|
||||
u->reboot = mg_millis() + 100;
|
||||
case 0x2122: // SET_CONTROL_LINE_STATE
|
||||
s_usb.dtr = value & 1;
|
||||
break;
|
||||
case 0x2123: // SEND_BREAK
|
||||
break;
|
||||
@@ -238,32 +327,12 @@ int mg_usb_setup(struct mg_usb *u, const uint8_t *r, const void **reply) {
|
||||
}
|
||||
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
size_t mg_usb_notify(struct mg_usb *u, const void **msg) {
|
||||
if (!u->notify) return 0;
|
||||
if (u->notify == 1 && u->alt) return u->notify = 2, *msg = s_speed, 16;
|
||||
u->notify = 0, *msg = u->alt ? s_up : s_down;
|
||||
return 8;
|
||||
}
|
||||
|
||||
size_t mg_usb_encode(struct mg_usb *u, uint8_t *p, const void *buf,
|
||||
size_t len) {
|
||||
if (!u->alt || !u->filter || len < 14 || len > 1514 ||
|
||||
len + 30 > u->input_size)
|
||||
return 0;
|
||||
memcpy(p, s_nth, sizeof(s_nth));
|
||||
MG_STORE_LE16(p + 6, u->sequence);
|
||||
MG_STORE_LE16(p + 8, len + 30);
|
||||
MG_STORE_LE16(p + 22, len);
|
||||
memcpy(p + 30, buf, len);
|
||||
u->sequence++;
|
||||
return len + 30;
|
||||
}
|
||||
|
||||
// Queue the frames of a received transfer block to the TCP/IP stack.
|
||||
// Offsets come from the host: bound every datagram by the block, and follow
|
||||
// at most 16 datagram tables so that a cyclic chain cannot loop forever
|
||||
void mg_usb_decode(struct mg_usb *u, const uint8_t *p, size_t len) {
|
||||
static void mgi_usb_decode(const uint8_t *p, size_t len) {
|
||||
size_t block, ndp, end, i, ofs, n, tables = 0;
|
||||
if (len < 12 || MG_LOAD_LE32(p) != 0x484d434e) return; // "NCMH"
|
||||
if (s_usb.ifp == NULL || len < 12 || MG_LOAD_LE32(p) != 0x484d434e) return;
|
||||
block = MG_LOAD_LE16(p + 8), ndp = MG_LOAD_LE16(p + 10);
|
||||
if (block > len) return;
|
||||
while (ndp && tables++ < 16 && ndp + 16 <= block &&
|
||||
@@ -273,27 +342,164 @@ void mg_usb_decode(struct mg_usb *u, const uint8_t *p, size_t len) {
|
||||
ofs = MG_LOAD_LE16(p + i), n = MG_LOAD_LE16(p + i + 2);
|
||||
if (ofs == 0 || n == 0) break;
|
||||
if (n >= 14 && n <= 1514 && ofs + n <= block)
|
||||
mg_tcpip_qwrite((void *) (p + ofs), n, u->ifp);
|
||||
mg_tcpip_qwrite((void *) (p + ofs), n, s_usb.ifp);
|
||||
}
|
||||
ndp = MG_LOAD_LE16(p + ndp + 6);
|
||||
}
|
||||
}
|
||||
|
||||
// If the Ethernet IN endpoint is idle, start sending the first queued
|
||||
// transfer block. It stays queued until sent. Drop blocks the host cannot
|
||||
// take now: the link closed, or it lowered its block size
|
||||
static void mgi_usb_tx_next(void) {
|
||||
char *block;
|
||||
size_t len;
|
||||
while (!s_usb.in[MG_USB_EP_ETH_DATA].busy &&
|
||||
(len = mg_queue_next(&s_usb.txq, &block)) > 0) {
|
||||
if (s_usb.alt && s_usb.filter && len <= s_usb.input_size) {
|
||||
mgi_usb_in_start(MG_USB_EP_ETH_DATA, block, len, len < s_usb.input_size);
|
||||
} else {
|
||||
mg_queue_del(&s_usb.txq, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
void mg_usb_log(char ch, void *arg) {
|
||||
struct mg_usb *u = (struct mg_usb *) arg;
|
||||
if (u->head - u->tail >= MG_USB_LOG_SIZE) u->tail++;
|
||||
u->log[u->head++ % MG_USB_LOG_SIZE] = ch;
|
||||
// Log output function: keep the latest MG_USB_LOG_SIZE characters
|
||||
static void mgi_usb_log(char ch, void *arg) {
|
||||
if (s_usb.head - s_usb.tail >= MG_USB_LOG_SIZE) s_usb.tail++;
|
||||
s_usb.log[s_usb.head++ % MG_USB_LOG_SIZE] = ch;
|
||||
(void) arg;
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t mg_usb_log_read(struct mg_usb *u, void *buf, size_t len) {
|
||||
size_t n = 0;
|
||||
while (n < len && u->tail != u->head) {
|
||||
((char *) buf)[n++] = u->log[u->tail++ % MG_USB_LOG_SIZE];
|
||||
// Controller events, see enum mg_usb_ev
|
||||
static int mgi_usb_event(enum mg_usb_ev ev, enum mg_usb_ep ep, void *buf,
|
||||
size_t len) {
|
||||
const uint8_t *p = (const uint8_t *) buf;
|
||||
unsigned i;
|
||||
if (ev == MG_USB_EV_RESET) {
|
||||
mgi_usb_unconfigure();
|
||||
for (i = 0; i < 5; i++) mgi_usb_idle(i);
|
||||
} else if (ev == MG_USB_EV_SETUP) {
|
||||
const void *reply;
|
||||
unsigned wlen = len < 8 ? 0 : MG_LOAD_LE16(p + 6);
|
||||
int n;
|
||||
mgi_usb_idle(MG_USB_EP_CTL); // A SETUP ends the previous transfer
|
||||
if (len < 8 || (!(p[0] & 0x80) && len < 8 + wlen)) return -1;
|
||||
if ((n = mgi_usb_request(p, &reply)) < 0) return -1;
|
||||
if (!(p[0] & 0x80) || wlen == 0) return 0;
|
||||
mgi_usb_in_start(MG_USB_EP_CTL, reply, (size_t) n, n < (int) wlen);
|
||||
return 1;
|
||||
} else if (ev == MG_USB_EV_IN && s_usb.in[ep].busy) {
|
||||
s_usb.in[ep].busy = mgi_usb_in_next(ep);
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (ep == MG_USB_EP_ETH_DATA && !s_usb.in[ep].busy) {
|
||||
mg_queue_del(&s_usb.txq, mg_queue_next(&s_usb.txq, NULL)); // Sent
|
||||
mgi_usb_tx_next();
|
||||
}
|
||||
#endif
|
||||
return s_usb.in[ep].busy;
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
} else if (ev == MG_USB_EV_OUT && ep == MG_USB_EP_ETH_DATA) {
|
||||
// Add the packet to the transfer block. At the advertised maximum OUT
|
||||
// size, the host need not send a ZLP. Other blocks end with a short
|
||||
// packet, possibly empty
|
||||
if (!s_usb.discard && s_usb.rxlen + len <= sizeof(s_usb.rx)) {
|
||||
memcpy(s_usb.rx + s_usb.rxlen, buf, len);
|
||||
s_usb.rxlen += len;
|
||||
} else {
|
||||
s_usb.discard = true;
|
||||
}
|
||||
if (len < 64 || s_usb.rxlen == sizeof(s_usb.rx)) {
|
||||
if (!s_usb.discard) mgi_usb_decode(s_usb.rx, s_usb.rxlen);
|
||||
s_usb.rxlen = 0, s_usb.discard = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool mg_usb_init(struct mg_tcpip_if *ifp, const struct mg_usb_hw *hw) {
|
||||
if (s_usb.hw == NULL) {
|
||||
s_usb.hw = hw;
|
||||
mgi_usb_unconfigure();
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
mg_queue_init(&s_usb.txq, s_usb.txq_buf, sizeof(s_usb.txq_buf));
|
||||
#endif
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
mg_log_set_fn(mgi_usb_log, NULL); // Log to the USB serial port
|
||||
#endif
|
||||
hw->init(s_usb.serial);
|
||||
}
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (ifp != NULL) s_usb.ifp = ifp;
|
||||
#else
|
||||
(void) ifp;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t mg_usb_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
// Queue the frame wrapped in its transfer block, ready to send
|
||||
size_t n = sizeof(s_nth) + len;
|
||||
char *p;
|
||||
if (!s_usb.alt || !s_usb.filter || len < 14 || len > 1514) return 0;
|
||||
if (mg_queue_next(&s_usb.txq, NULL) == 0) {
|
||||
mg_queue_init(&s_usb.txq, s_usb.txq.buf, s_usb.txq.size); // Unwrap
|
||||
}
|
||||
if (mg_queue_book(&s_usb.txq, &p, n) < n) {
|
||||
ifp->nerr++; // Queue full: the host is not reading
|
||||
return 0;
|
||||
}
|
||||
memcpy(p, s_nth, sizeof(s_nth));
|
||||
MG_STORE_LE16(p + 6, s_usb.sequence);
|
||||
s_usb.sequence++;
|
||||
MG_STORE_LE16(p + 8, n);
|
||||
MG_STORE_LE16(p + 22, len);
|
||||
memcpy(p + sizeof(s_nth), buf, len);
|
||||
mg_queue_add(&s_usb.txq, n);
|
||||
mgi_usb_tx_next();
|
||||
return len;
|
||||
#else
|
||||
(void) ifp, (void) buf, (void) len;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool mg_usb_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
(void) ifp;
|
||||
if (s_usb.hw == NULL) return false; // mg_usb_init() not called yet
|
||||
s_usb.hw->poll(mgi_usb_event);
|
||||
#if MG_ENABLE_USB_SERIAL
|
||||
if (s_usb.configured && s_usb.dtr && !s_usb.in[MG_USB_EP_SER_DATA].busy) {
|
||||
size_t n = 0;
|
||||
while (n < sizeof(s_usb.out) && s_usb.tail != s_usb.head) {
|
||||
s_usb.out[n++] = s_usb.log[s_usb.tail++ % MG_USB_LOG_SIZE];
|
||||
}
|
||||
if (n > 0) mgi_usb_in_start(MG_USB_EP_SER_DATA, s_usb.out, n, true);
|
||||
}
|
||||
#endif
|
||||
#if MG_ENABLE_USB_ETHERNET
|
||||
if (s_usb.notify && !s_usb.in[MG_USB_EP_ETH_EV].busy) {
|
||||
// Link notifications: the speed first if the link is up, then the state
|
||||
if (s_usb.notify == 1 && s_usb.alt) {
|
||||
s_usb.notify = 2;
|
||||
mgi_usb_in_start(MG_USB_EP_ETH_EV, s_speed, sizeof(s_speed), false);
|
||||
} else {
|
||||
s_usb.notify = 0;
|
||||
mgi_usb_in_start(MG_USB_EP_ETH_EV, s_usb.alt ? s_up : s_down,
|
||||
sizeof(s_up), false);
|
||||
}
|
||||
}
|
||||
mgi_usb_tx_next();
|
||||
return s1 && s_usb.alt && s_usb.filter;
|
||||
#else
|
||||
(void) s1;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user