diff --git a/flake.nix b/flake.nix index 09b5d93..573cdeb 100644 --- a/flake.nix +++ b/flake.nix @@ -77,7 +77,7 @@ }; monolith = lib.nixosSystem { inherit system specialArgs; - modules = [ ./hosts/monolith.nix ./system/gitlab-runner.nix ] + modules = [ ./hosts/monolith.nix ./system/gitlab-runner.nix ./system/btusb-kernel-patches.nix ] ++ common_modules; }; rainbow = lib.nixosSystem { diff --git a/system/btusb-kernel-patches.nix b/system/btusb-kernel-patches.nix new file mode 100644 index 0000000..871b1ff --- /dev/null +++ b/system/btusb-kernel-patches.nix @@ -0,0 +1,21 @@ +{ config, pkgs, lib, inputs, ... }: { + boot.kernelPackages = lib.mkDefault pkgs.linuxPackages_latest; + boot.kernelPatches = [ + { + name = "bt-usb-fixes-1"; + patch = ./kernel/patches/1-3-Bluetooth-btusb-Fix-Chinese-CSR-dongles-again-by-re-adding-ERR_DATA_REPORTING-quirk.diff; + } + { + name = "bt-usb-fixes-2"; + patch = ./kernel/patches/2-3-Bluetooth-btusb-Add-a-setup-message-for-CSR-dongles-showing-the-Read-Local-Information-values.diff; + } + { + name = "bt-usb-fixes-3"; + patch = ./kernel/patches/3-3-Bluetooth-btusb-Add-a-parameter-to-let-users-disable-the-fake-CSR-force-suspend-hack.diff; + } + { + name = "bt-usb-fixes-4"; + patch = ./kernel/patches/fix-btusb-crash.diff; + } + ]; +} diff --git a/system/kernel/patches/1-3-Bluetooth-btusb-Fix-Chinese-CSR-dongles-again-by-re-adding-ERR_DATA_REPORTING-quirk.diff b/system/kernel/patches/1-3-Bluetooth-btusb-Fix-Chinese-CSR-dongles-again-by-re-adding-ERR_DATA_REPORTING-quirk.diff new file mode 100644 index 0000000..2890e64 --- /dev/null +++ b/system/kernel/patches/1-3-Bluetooth-btusb-Fix-Chinese-CSR-dongles-again-by-re-adding-ERR_DATA_REPORTING-quirk.diff @@ -0,0 +1,68 @@ +diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c +index 3b269060e91f..1360b2163ec5 100644 +--- a/drivers/bluetooth/btusb.c ++++ b/drivers/bluetooth/btusb.c +@@ -2174,6 +2174,7 @@ static int btusb_setup_csr(struct hci_dev *hdev) + * without these the controller will lock up. + */ + set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks); ++ set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks); + set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks); + set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks); + +diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h +index e004ba04a9ae..0fe789f6a653 100644 +--- a/include/net/bluetooth/hci.h ++++ b/include/net/bluetooth/hci.h +@@ -228,6 +228,17 @@ enum { + */ + HCI_QUIRK_VALID_LE_STATES, + ++ /* When this quirk is set, then erroneous data reporting ++ * is ignored. This is mainly due to the fact that the HCI ++ * Read Default Erroneous Data Reporting command is advertised, ++ * but not supported; these controllers often reply with unknown ++ * command and tend to lock up randomly. Needing a hard reset. ++ * ++ * This quirk can be set before hci_register_dev is called or ++ * during the hdev->setup vendor callback. ++ */ ++ HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, ++ + /* + * When this quirk is set, then the hci_suspend_notifier is not + * registered. This is intended for devices which drop completely +diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c +index bd9eb713b26b..0a7abc817f10 100644 +--- a/net/bluetooth/hci_sync.c ++++ b/net/bluetooth/hci_sync.c +@@ -3798,7 +3798,8 @@ static int hci_read_page_scan_activity_sync(struct hci_dev *hdev) + static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev) + { + if (!(hdev->commands[18] & 0x04) || +- !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING)) ++ !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) || ++ test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) + return 0; + + return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING, +@@ -4316,7 +4317,8 @@ static int hci_set_err_data_report_sync(struct hci_dev *hdev) + bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED); + + if (!(hdev->commands[18] & 0x08) || +- !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING)) ++ !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) || ++ test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) + return 0; + + if (enabled == hdev->err_data_reporting) +@@ -4475,6 +4477,9 @@ static const struct { + HCI_QUIRK_BROKEN(STORED_LINK_KEY, + "HCI Delete Stored Link Key command is advertised, " + "but not supported."), ++ HCI_QUIRK_BROKEN(ERR_DATA_REPORTING, ++ "HCI Read Default Erroneous Data Reporting command is " ++ "advertised, but not supported."), + HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER, + "HCI Read Transmit Power Level command is advertised, " + "but not supported."), diff --git a/system/kernel/patches/2-3-Bluetooth-btusb-Add-a-setup-message-for-CSR-dongles-showing-the-Read-Local-Information-values.diff b/system/kernel/patches/2-3-Bluetooth-btusb-Add-a-setup-message-for-CSR-dongles-showing-the-Read-Local-Information-values.diff new file mode 100644 index 0000000..9307833 --- /dev/null +++ b/system/kernel/patches/2-3-Bluetooth-btusb-Add-a-setup-message-for-CSR-dongles-showing-the-Read-Local-Information-values.diff @@ -0,0 +1,16 @@ +diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c +index 1360b2163ec5..8f34bf195bae 100644 +--- a/drivers/bluetooth/btusb.c ++++ b/drivers/bluetooth/btusb.c +@@ -2112,6 +2112,11 @@ static int btusb_setup_csr(struct hci_dev *hdev) + + rp = (struct hci_rp_read_local_version *)skb->data; + ++ bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x; LMP ver=%u subver=%04x; manufacturer=%u", ++ le16_to_cpu(rp->hci_ver), le16_to_cpu(rp->hci_rev), ++ le16_to_cpu(rp->lmp_ver), le16_to_cpu(rp->lmp_subver), ++ le16_to_cpu(rp->manufacturer)); ++ + /* Detect a wide host of Chinese controllers that aren't CSR. + * + * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891 diff --git a/system/kernel/patches/3-3-Bluetooth-btusb-Add-a-parameter-to-let-users-disable-the-fake-CSR-force-suspend-hack.diff b/system/kernel/patches/3-3-Bluetooth-btusb-Add-a-parameter-to-let-users-disable-the-fake-CSR-force-suspend-hack.diff new file mode 100644 index 0000000..7d2057f --- /dev/null +++ b/system/kernel/patches/3-3-Bluetooth-btusb-Add-a-parameter-to-let-users-disable-the-fake-CSR-force-suspend-hack.diff @@ -0,0 +1,67 @@ +diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c +index 8f34bf195bae..d31d4f925463 100644 +--- a/drivers/bluetooth/btusb.c ++++ b/drivers/bluetooth/btusb.c +@@ -34,6 +34,7 @@ static bool force_scofix; + static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND); + static bool enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC); + static bool reset = true; ++static bool disable_fake_csr_forcesuspend_hack; + + static struct usb_driver btusb_driver; + +@@ -2171,7 +2172,7 @@ static int btusb_setup_csr(struct hci_dev *hdev) + is_fake = true; + + if (is_fake) { +- bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once..."); ++ bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds..."); + + /* Generally these clones have big discrepancies between + * advertised features and what's actually supported. +@@ -2215,21 +2216,24 @@ static int btusb_setup_csr(struct hci_dev *hdev) + * apply this initialization quirk to every controller that gets here, + * it should be harmless. The alternative is to not work at all. + */ +- pm_runtime_allow(&data->udev->dev); ++ if (!disable_fake_csr_forcesuspend_hack) { ++ bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; force-suspending once..."); ++ pm_runtime_allow(&data->udev->dev); + +- ret = pm_runtime_suspend(&data->udev->dev); +- if (ret >= 0) +- msleep(200); +- else +- bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround"); ++ ret = pm_runtime_suspend(&data->udev->dev); ++ if (ret >= 0) ++ msleep(200); ++ else ++ bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround"); + +- pm_runtime_forbid(&data->udev->dev); ++ pm_runtime_forbid(&data->udev->dev); + +- device_set_wakeup_capable(&data->udev->dev, false); ++ device_set_wakeup_capable(&data->udev->dev, false); + +- /* Re-enable autosuspend if this was requested */ +- if (enable_autosuspend) +- usb_enable_autosuspend(data->udev); ++ /* Re-enable autosuspend if this was requested */ ++ if (enable_autosuspend) ++ usb_enable_autosuspend(data->udev); ++ } + } + + kfree_skb(skb); +@@ -4312,6 +4316,9 @@ MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default"); + module_param(reset, bool, 0644); + MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); + ++module_param(disable_fake_csr_forcesuspend_hack, bool, 0644); ++MODULE_PARM_DESC(disable_fake_csr_forcesuspend_hack, "Don't indiscriminately force-suspend Chinese-cloned CSR dongles trying to unfreeze them"); ++ + MODULE_AUTHOR("Marcel Holtmann "); + MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION); + MODULE_VERSION(VERSION); diff --git a/system/kernel/patches/fix-btusb-crash.diff b/system/kernel/patches/fix-btusb-crash.diff new file mode 100644 index 0000000..cc371d5 --- /dev/null +++ b/system/kernel/patches/fix-btusb-crash.diff @@ -0,0 +1,27 @@ +diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c +index 6ae5aa5..3c5e80f 100644 +--- a/net/bluetooth/hci_core.c ++++ b/net/bluetooth/hci_core.c +@@ -2757,7 +2757,8 @@ int hci_register_suspend_notifier(struct hci_dev *hdev) + { + int ret = 0; + +- if (!test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks)) { ++ if (!hdev->suspend_notifier.notifier_call && ++ !test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks)) { + hdev->suspend_notifier.notifier_call = hci_suspend_notifier; + ret = register_pm_notifier(&hdev->suspend_notifier); + } +@@ -2769,8 +2770,11 @@ int hci_unregister_suspend_notifier(struct hci_dev *hdev) + { + int ret = 0; + +- if (!test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks)) ++ if (hdev->suspend_notifier.notifier_call) { + ret = unregister_pm_notifier(&hdev->suspend_notifier); ++ if (!ret) ++ hdev->suspend_notifier.notifier_call = NULL; ++ } + + return ret; + }