summaryrefslogtreecommitdiff
path: root/libusb
diff options
context:
space:
mode:
Diffstat (limited to 'libusb')
-rw-r--r--libusb/CMakeLists.txt7
-rw-r--r--libusb/include/libusb/tusb_helpers.h10
-rwxr-xr-xlibusb/include/libusb/tusb_helpers.h.gen29
-rw-r--r--libusb/include/libusb/usb_common.h2
-rw-r--r--libusb/usb_common.c73
5 files changed, 68 insertions, 53 deletions
diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt
index f5d6ae2..b11e798 100644
--- a/libusb/CMakeLists.txt
+++ b/libusb/CMakeLists.txt
@@ -1,17 +1,16 @@
# libusb/CMakeLists.txt - Build script for libusb support library
#
-# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+# Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
add_library(libusb INTERFACE)
-target_include_directories(libusb SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
+target_include_directories(libusb PUBLIC INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
target_sources(libusb INTERFACE
usb_common.c
)
target_link_libraries(libusb INTERFACE
- pico_unique_id
tinyusb_device
- tinyusb_board
libcr_ipc
+ libmisc
)
diff --git a/libusb/include/libusb/tusb_helpers.h b/libusb/include/libusb/tusb_helpers.h
index a250ca7..0c35f62 100644
--- a/libusb/include/libusb/tusb_helpers.h
+++ b/libusb/include/libusb/tusb_helpers.h
@@ -1,7 +1,7 @@
-/* Generated by `./libusb/include/libusb/tusb_helpers.h.gen `. DO NOT EDIT! */
+/* Generated by `libusb/include/libusb/tusb_helpers.h.gen `. DO NOT EDIT! */
/* libusb/tusb_helpers.h - Preprocessor macros that I think should be included in TinyUSB
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
*
* SPDX-License-Identifier: MIT
*
@@ -36,16 +36,16 @@
* dated 2000-03-29. There is no longer any mention of this on usb.org, but I found a copy at
* http://www.baiheee.com/Documents/090518/090518112619/USB_LANGIDs.pdf
*
- * So how does the USB-IF defined LANGIDs these days?
+ * So if they don't publish that anymore, how does the USB-IF define LANGIDs these days?
*
* https://www.usb.org/deprecated-links-and-tools says "To get the latest LANGID definitions go to
* https://docs.microsoft.com/en-us/windows/desktop/intl/language-identifier-constants-and-strings. This
* page will change as new LANGIDs are added." That page has no list of LANGIDs, but says "For the
* predefined primary language identifiers with their valid sublanguage identifiers, see
* [\[MS-LCID\]: Windows Language Code Identifier (LCID)
- * Reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-565326e84c37f)."
+ * Reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f)."
* That page at the time of this writing as a PDF marked as version 16.0, dated 2024-04-24:
- * https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-LCID/%5bMS-LCID%5d.pdf
+ * https://winprotocoldocs-bhdugrdyduf5h2e4.b02.azurefd.net/MS-LCID/%5bMS-LCID%5d.pdf
* [MS-LCID] defines an LCID as a 32-bit value consisting of a 16-bit a "Language ID", a 4-bit "Sort
* ID", and 12 reserved bits.
*
diff --git a/libusb/include/libusb/tusb_helpers.h.gen b/libusb/include/libusb/tusb_helpers.h.gen
index 326ca22..1de1d09 100755
--- a/libusb/include/libusb/tusb_helpers.h.gen
+++ b/libusb/include/libusb/tusb_helpers.h.gen
@@ -7,7 +7,7 @@ echo "/* Generated by \`$0 $*\`. DO NOT EDIT! */"
cat <<'EOT'
/* libusb/tusb_helpers.h - Preprocessor macros that I think should be included in TinyUSB
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
*
* SPDX-License-Identifier: MIT
*
@@ -42,16 +42,16 @@ cat <<'EOT'
* dated 2000-03-29. There is no longer any mention of this on usb.org, but I found a copy at
* http://www.baiheee.com/Documents/090518/090518112619/USB_LANGIDs.pdf
*
- * So how does the USB-IF defined LANGIDs these days?
+ * So if they don't publish that anymore, how does the USB-IF define LANGIDs these days?
*
* https://www.usb.org/deprecated-links-and-tools says "To get the latest LANGID definitions go to
* https://docs.microsoft.com/en-us/windows/desktop/intl/language-identifier-constants-and-strings. This
* page will change as new LANGIDs are added." That page has no list of LANGIDs, but says "For the
* predefined primary language identifiers with their valid sublanguage identifiers, see
* [\[MS-LCID\]: Windows Language Code Identifier (LCID)
- * Reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-565326e84c37f)."
+ * Reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f)."
* That page at the time of this writing as a PDF marked as version 16.0, dated 2024-04-24:
- * https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-LCID/%5bMS-LCID%5d.pdf
+ * https://winprotocoldocs-bhdugrdyduf5h2e4.b02.azurefd.net/MS-LCID/%5bMS-LCID%5d.pdf
* [MS-LCID] defines an LCID as a 32-bit value consisting of a 16-bit a "Language ID", a 4-bit "Sort
* ID", and 12 reserved bits.
*
@@ -65,17 +65,24 @@ cat <<'EOT'
*/
EOT
if [[ ! -f 3rd-party/MS-LCID.pdf || 3rd-party/MS-LCID.pdf -ot $0 ]]; then
- wget --no-use-server-timestamps -O 3rd-party/MS-LCID.pdf 'https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-LCID/%5bMS-LCID%5d.pdf'
+ wget --no-use-server-timestamps -O 3rd-party/MS-LCID.pdf 'https://winprotocoldocs-bhdugrdyduf5h2e4.b02.azurefd.net/MS-LCID/%5bMS-LCID%5d.pdf'
fi
if [[ ! -f 3rd-party/MS-LCID.txt || 3rd-party/MS-LCID.txt -ot 3rd-party/MS-LCID.pdf ]]; then
pdftotext -layout 3rd-party/MS-LCID.pdf
fi
-<3rd-party/MS-LCID.txt \
- grep -E '^\s*0x[0-9A-F]{4}\s+[a-z]' | sed 's/,.*//' | grep -v reserved | # find the lines we're interested in
- sed -E 's/^\s*0x(..)(..)\s+(\S.*)/\2 \1 \3/p' | tr '[:lower:]-' '[:upper:]_' | # format them as 'PRIhex SUBhex UPPER_STR'
- sort |
- sed -E 's/(..) (..) (.*)/#define LANGID_\3 0x\2\1/' | # format them as '#define LANGID_UPPER_STR 0xSUBPRI'
- column --table --output-separator=' '
+{
+ {
+ # find the lines we're interested in
+ grep -E '^\s*0x[0-9A-F]{4}\s+[a-z]' | sed 's/,.*//' | grep -v reserved
+ } | {
+ # format them as 'PRIhex SUBhex UPPER_STR'
+ sed -E 's/^\s*0x(..)(..)\s+(\S.*)/\2 \1 \3/p' | tr '[:lower:]-' '[:upper:]_'
+ } | sort | {
+ # format them as '#define LANGID_UPPER_STR 0xSUBPRI'
+ sed -E 's/(..) (..) (.*)/#define LANGID_\3 0x\2\1/' |
+ column --table --output-separator=' '
+ }
+} <3rd-party/MS-LCID.txt
cat <<'EOT'
/** USB 2.0 §9.6.6 "Endpoint", field bEndpointAddress, bit 7 */
diff --git a/libusb/include/libusb/usb_common.h b/libusb/include/libusb/usb_common.h
index e31b3ab..9fe0794 100644
--- a/libusb/include/libusb/usb_common.h
+++ b/libusb/include/libusb/usb_common.h
@@ -27,7 +27,7 @@ enum {
/* Globals ********************************************************************/
extern uint8_t cfgnum_std;
-void usb_common_earlyinit(void);
+void usb_common_earlyinit(uint16_t *serial, uint8_t serial_bytelen);
void usb_common_lateinit(void);
COROUTINE usb_common_cr(void *arg);
diff --git a/libusb/usb_common.c b/libusb/usb_common.c
index ceabad3..29dec42 100644
--- a/libusb/usb_common.c
+++ b/libusb/usb_common.c
@@ -6,15 +6,32 @@
#include <stdint.h> /* for uint{n}_t types */
#include <stddef.h> /* for size_t */
-#include <string.h> /* memcpy(newlib) */
-#include <assert.h> /* for assert(newlib) */
-#include <stdlib.h> /* for malloc(pico_malloc), realloc(pico_malloc), reallocarray(pico_malloc) */
-#include "bsp/board_api.h" /* for board_init(), board_init_after_usb(), board_usb_get_serial(TinyUSB) */
-#include "tusb.h" /* for various tusb_*_t types */
+#include <string.h> /* memcpy() */
+#include <stdlib.h> /* for malloc(), realloc(), reallocarray() */
+
+#include <tusb.h> /* for various tusb_*_t types */
+
+#include <libmisc/assert.h>
+
+#define LOG_NAME USB_COMMON
+#include <libmisc/log.h>
#include <libusb/tusb_helpers.h> /* for LANGID_*, TU_UTF16() */
#include <libusb/usb_common.h>
+#include "config.h"
+
+/* Globals ********************************************************************/
+
+uint8_t cfgnum_std = 0;
+
+static struct {
+ uint8_t configc;
+ uint8_t **configv;
+ uint16_t *serial;
+ uint8_t serial_bytelen;
+} globals = {0};
+
/* Strings ********************************************************************/
/**
@@ -47,18 +64,16 @@ uint16_t const *tud_descriptor_string_cb(uint8_t strid, uint16_t langid) {
case STRID_CFG: CONST_STR(TU_UTF16("Standard Configuration")); break;
case STRID_KBD_IFC: CONST_STR(TU_UTF16("Keyboard Interface")); break;
case STRID_SERIAL:
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
- bytelen = 2 * board_usb_get_serial(desc.bString, TU_ARRAY_SIZE(desc.bString));
-#pragma GCC diagnostic pop
+ bytelen = globals.serial_bytelen;
+ memcpy(desc.bString, globals.serial, bytelen);
break;
default:
- printf("GET STRING: unknown string id=%"PRIu8, strid);
+ debugf("GET STRING: unknown string id=%"PRIu8, strid);
return NULL;
}
break;
default:
- printf("GET STRING: unknown LANGID=%"PRIx16, langid);
+ debugf("GET STRING: unknown LANGID=%"PRIx16, langid);
return NULL;
}
}
@@ -68,11 +83,11 @@ uint16_t const *tud_descriptor_string_cb(uint8_t strid, uint16_t langid) {
return (uint16_t *)&desc;
}
-/* Globals ********************************************************************/
-
-uint8_t cfgnum_std = 0;
+/* Main entrypoints ***********************************************************/
-void usb_common_earlyinit(void) {
+void usb_common_earlyinit(uint16_t *serial, uint8_t serial_bytelen) {
+ globals.serial = serial;
+ globals.serial_bytelen = serial_bytelen;
if (cfgnum_std)
return;
cfgnum_std = usb_add_config(
@@ -82,10 +97,7 @@ void usb_common_earlyinit(void) {
}
void usb_common_lateinit(void) {
- board_init();
- tud_init(BOARD_TUD_RHPORT);
- if (board_init_after_tusb)
- board_init_after_tusb();
+ tud_init(CONFIG_USB_COMMON_RHPORT);
}
COROUTINE usb_common_cr(void *_arg) {
@@ -102,19 +114,16 @@ COROUTINE usb_common_cr(void *_arg) {
/* Main utilities *************************************************************/
-static uint8_t configc = 0;
-static uint8_t **configv = NULL;
-
uint8_t usb_add_config(uint8_t iConfiguration, uint8_t bmAttributes, uint8_t bMaxPower_mA) {
uint8_t *desc;
- configv = reallocarray(configv, ++configc, sizeof configv[0]);
- desc = configv[configc-1] = malloc(TUD_CONFIG_DESC_LEN);
+ globals.configv = reallocarray(globals.configv, ++globals.configc, sizeof globals.configv[0]);
+ desc = globals.configv[globals.configc-1] = malloc(TUD_CONFIG_DESC_LEN);
memcpy(desc, (uint8_t[]){
/* USB configuration descriptor header (USB 2.0 §9.6.4 "Configuration") */
TUD_CONFIG_DESCRIPTOR(
- configc, /* bConfigurationValue */
+ globals.configc, /* bConfigurationValue */
0, /* bNumInterfaces (will be incremented by usb_add_interface() */
iConfiguration, /* iConfiguration */
TUD_CONFIG_DESC_LEN, /* wTotalLength (will be incremented by usb_add_interface() */
@@ -122,15 +131,15 @@ uint8_t usb_add_config(uint8_t iConfiguration, uint8_t bmAttributes, uint8_t bMa
bMaxPower_mA+1), /* bMaxPower (+1 because tusb just does n/2 instead of (n+1)/2) */
}, TUD_CONFIG_DESC_LEN);
- return configc;
+ return globals.configc;
}
uint8_t usb_add_interface(uint8_t cfg_num, uint16_t ifc_len, uint8_t *ifc_dat) {
- assert(cfg_num > 0 && cfg_num <= configc);
+ assert(cfg_num > 0 && cfg_num <= globals.configc);
assert(ifc_len >= 3);
assert(ifc_dat);
- uint8_t *desc = configv[cfg_num-1];
+ uint8_t *desc = globals.configv[cfg_num-1];
// wTotalLength
uint16_t total_len = TU_U16(desc[3], desc[2]) + ifc_len;
desc[3] = TU_U16_HIGH(total_len);
@@ -138,7 +147,7 @@ uint8_t usb_add_interface(uint8_t cfg_num, uint16_t ifc_len, uint8_t *ifc_dat) {
// bNumInterfaces
ifc_dat[3] = desc[4]++;
- desc = configv[cfg_num-1] = realloc(desc, total_len);
+ desc = globals.configv[cfg_num-1] = realloc(desc, total_len);
memcpy(&desc[total_len-ifc_len], ifc_dat, ifc_len);
return ifc_dat[3];
@@ -175,7 +184,7 @@ uint8_t const *tud_descriptor_device_cb(void) {
.bNumConfigurations = 0, /* Number of possible configurations */
};
- desc.bNumConfigurations = configc;
+ desc.bNumConfigurations = globals.configc;
return (uint8_t const *) &desc;
}
@@ -184,7 +193,7 @@ uint8_t const *tud_descriptor_device_cb(void) {
* USB 2.0 §9.6.4 "Configuration") for the given index.
*/
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
- if (index >= configc)
+ if (index >= globals.configc)
return NULL;
- return configv[index];
+ return globals.configv[index];
}