Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host OS detection quirk. #2628

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function(add_tinyusb TARGET)
# common
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tusb.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/common/tusb_fifo.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/common/tusb_quirk.c
# device
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd_control.c
Expand Down
75 changes: 75 additions & 0 deletions src/common/tusb_quirk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 HiFiPhile
*
* 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.
*
*/

#include "tusb_quirk.h"

#if CFG_TUD_QUIRK_HOST_OS_HINT
static tusb_desc_type_t desc_req_buf[2];
static int desc_req_idx = 0;

void tud_quirk_host_os_hint_desc_cb(tusb_desc_type_t desc) {
if (desc == TUSB_DESC_DEVICE) {
desc_req_idx = 0;
} else if (desc_req_idx < 2 && (desc == TUSB_DESC_CONFIGURATION || desc == TUSB_DESC_BOS || desc == TUSB_DESC_STRING)) {
// Skip redundant request
if (desc_req_idx == 0 || (desc_req_idx > 0 && desc_req_buf[desc_req_idx - 1] != desc)) {
desc_req_buf[desc_req_idx++] = desc;
}
}
}

// Each OS request descriptors differently:
// Windows 10 - 11
// Device Desc
// Config Desc
// BOS Desc
// String Desc
// Linux 4.14 - 6.8
// Device Desc
// BOS Desc
// Config Desc
// String Desc
// OS X
// Device Desc
// String Desc
// Config Desc || BOS Desc
// BOS Desc || Config Desc
tud_quirk_host_os_t tud_quirk_host_os_hint(void) {
if (desc_req_idx < 2) {
return TUD_QUIRK_OS_HINT_UNKNOWN;
}

if (desc_req_buf[0] == TUSB_DESC_BOS && desc_req_buf[1] == TUSB_DESC_CONFIGURATION) {
return TUD_QUIRK_OS_HINT_LINUX;
} else if (desc_req_buf[0] == TUSB_DESC_CONFIGURATION && desc_req_buf[1] == TUSB_DESC_BOS) {
return TUD_QUIRK_OS_HINT_WINDOWS;
} else if (desc_req_buf[0] == TUSB_DESC_STRING && (desc_req_buf[1] == TUSB_DESC_BOS || desc_req_buf[1] == TUSB_DESC_CONFIGURATION)) {
return TUD_QUIRK_OS_HINT_OSX;
}

return TUD_QUIRK_OS_HINT_UNKNOWN;
}

#endif
74 changes: 74 additions & 0 deletions src/common/tusb_quirk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 HiFiPhile
*
* 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 _TUSB_QUIRK_H_
#define _TUSB_QUIRK_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "common/tusb_common.h"

//===================================== WARNING =========================================
// These quirks operate out of USB specification in order to workaround specific issues.
// They may not work on your platform.
//=======================================================================================

#ifndef CFG_TUD_QUIRK_HOST_OS_HINT
#define CFG_TUD_QUIRK_HOST_OS_HINT 0
#endif

// Host OS detection, can be used to adjust configuration to workaround host limits.
//
// Prerequisites:
// - Set USB version to at least 2.01 in Device Descriptor
// - Has a valid BOS Descriptor, refer to webusb_serial example
//
// Attention:
// Windows detection result comes out after Configuration Descriptor request,
// meaning it will be too late to do descriptor adjustment. It's advised to make
// Windows as default configuration and adjust to other OS accordingly.
#if CFG_TUD_QUIRK_HOST_OS_HINT
typedef enum {
TUD_QUIRK_OS_HINT_UNKNOWN,
TUD_QUIRK_OS_HINT_LINUX,
TUD_QUIRK_OS_HINT_OSX,
TUD_QUIRK_OS_HINT_WINDOWS,
} tud_quirk_host_os_t;

// Get Host OS type
tud_quirk_host_os_t tud_quirk_host_os_hint(void);

// Internal callback
void tud_quirk_host_os_hint_desc_cb(tusb_desc_type_t desc);
#endif


#ifdef __cplusplus
}
#endif

#endif /* _TUSB_QUIRK_H_ */
5 changes: 5 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "device/dcd.h"
#include "tusb.h"
#include "common/tusb_private.h"
#include "common/tusb_quirk.h"

#include "device/usbd.h"
#include "device/usbd_pvt.h"
Expand Down Expand Up @@ -967,6 +968,10 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
uint8_t const desc_index = tu_u16_low( p_request->wValue );

#if CFG_TUD_QUIRK_HOST_OS_HINT
tud_quirk_host_os_hint_desc_cb(desc_type);
#endif

switch(desc_type)
{
case TUSB_DESC_DEVICE:
Expand Down
1 change: 1 addition & 0 deletions src/tinyusb.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
TINYUSB_SRC_C += \
src/tusb.c \
src/common/tusb_fifo.c \
src/common/tusb_quirk.c \
src/device/usbd.c \
src/device/usbd_control.c \
src/typec/usbc.c \
Expand Down
1 change: 1 addition & 0 deletions src/tusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "common/tusb_common.h"
#include "osal/osal.h"
#include "common/tusb_fifo.h"
#include "common/tusb_quirk.h"

//------------- TypeC -------------//
#if CFG_TUC_ENABLED
Expand Down