diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-15 23:16:00 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-15 23:16:00 -0600 |
commit | 7c30fe0be98a3028964d437f5c31cc968fbf9755 (patch) | |
tree | 36047161fe75349acac0e179b8010205a7fe08bb /tusb_helpers.h.gen | |
parent | d297d8ec1f5bac1015c53dfe169d2ce1a838b007 (diff) |
wip
Diffstat (limited to 'tusb_helpers.h.gen')
-rwxr-xr-x | tusb_helpers.h.gen | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tusb_helpers.h.gen b/tusb_helpers.h.gen new file mode 100755 index 0000000..4682166 --- /dev/null +++ b/tusb_helpers.h.gen @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# -*- Mode: C -*- +set -e +exec >"${0%.gen}" +cat <<'EOT' +/* tusb_helpers.h - Preprocessor macros that I think should be included in TinyUSB + * + * Copyright (c) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef _USB_HELPERS_H_ +#define _USB_HELPERS_H_ + +/* USB 2.0 §9.6.7 "String" says "The list of currently defined USB LANGIDs can be found at + * http://www.usb.org/developers/docs.html.", but that page 404s. + * + * Once upon a time the USB-IF (usb.org) published a "Language Identifiers (LANGIDs)" version 1.0, + * 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? + * + * 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)." + * 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 + * [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. + * + * That is to say: The USB-IF has said in essence "USB LANGIDs are defined to be the 'Language ID' + * portion of Microsoft Windows LCIDs (Language Code Identifiers); refer to Microsoft's published + * list of LCID Language IDs for a list of USB LANGIDs." + * + * The scheme for Language IDs is that the least-significant-byte is "primary" ID and the + * most-significant-byte is a "sublanguage" ID within that. With this in mind, Microsoft's choice + * to sort their list of by most-significant-byte was a poor editorial choice. + */ +EOT +[ -f MS-LCID.pdf ] || wget -O MS-LCID.pdf 'https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-LCID/%5bMS-LCID%5d.pdf' +[ -f MS-LCID.txt ] || pdftotext -layout MS-LCID.pdf +<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=' ' +cat <<'EOT' + +/* USB 2.0 §9.6.6 "Endpoint", field bEndpointAddress, bit 7 */ +#define TUD_ENDPOINT_OUT 0x00 +#define TUD_ENDPOINT_IN 0x80 + +#define UTF16(str) u ## str + +static inline size_t utf16_strncpy(uint16_t *dst, uint16_t *src, dsize size_t) { + size_t i; + for (i = 0; i < dsize && src && src[i]; i++) + dst[i] = src[i]; + return i; +} + +#endif /* _USB_HELPERS_H_ */ +EOT |