Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp1244890rdb; Sun, 7 Jan 2024 10:04:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdcMOWnY/xtCOP4u9uaRIUFuynBF/rHUzn8Ve6ZGdr9Tijc2/ILtVOab+xVIqydJYhgCq8 X-Received: by 2002:a92:c564:0:b0:35f:dc78:48d5 with SMTP id b4-20020a92c564000000b0035fdc7848d5mr5424618ilj.52.1704650681892; Sun, 07 Jan 2024 10:04:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704650681; cv=none; d=google.com; s=arc-20160816; b=MYiAD66T4C9ZOf1GuTt9X/RUsrD2Ix9fmQQX16j0B3NBk2/VEPG7dgcdC+gaLXvTNn QlFZqZQzwcBHVIPSNXFAnb5x5pjslxl6K5P1aUwjMA9VIIj+vpF/LnLSAj3CoMm4w2KB IS4BDIE5/a06G+kwn0W0UvlTt6rK3rK6nyiVBlM+3BfOqte8E0KX29p7/FWdKTXI4VMU 07P7D1FUSj6w7V1Yzb4qnJc9B4GAMenQMXfnebA8Qrn3gpdOCW26mFbgxstgJqxb2FCM qayspXikLwsPEhWq7tRoIy4xLqvRE89S8QAlcASXl+fywOOZK9lnjBf0utlNQExEQCTd KUxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=ogvFWz8faKUl8w9Qkr6MIMsXTJbDcRdSuadInZT/gp4=; fh=S5rf2bc7VLlSXYQ0EqFAIodb1qEiUfsxulvZhQKZRws=; b=l/wAhJJO/DRzERpENbrkQaJvpREJYfdRcRLCK1AwFNkQzPUDTIZSUvF+GnWH6HLXar zWyciTpH+sJHzBMyyWH9L1HN9A6PtiOOjl/QZt9BhKJeyRCFJgWTT4vD/Ts9v/irc5Hv QSzA827lOmQr2aLlUkIjPmr7TYu9g0JBiX+56i3H2y+vI/FbN9lGoEOCzrLom2Pl05Dg xVo+vAq+J+wHDeoLVozOsFAW4PvtrwEgwbGOZNP73yMzPLOEhF37q3EcFMi6W/qyTcUI gU3yiN0yCsAMCR8lI9HJBLEp2RX6KF7BRCkrzAEXceJDSV5QsXotx7W7h2161vL9HQQW z4JQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18944-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18944-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y4-20020a170902700400b001d4f32b2764si3983317plk.330.2024.01.07.10.04.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jan 2024 10:04:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18944-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18944-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18944-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 481A4282219 for ; Sun, 7 Jan 2024 18:04:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 839102942A; Sun, 7 Jan 2024 18:03:19 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B4C414F64; Sun, 7 Jan 2024 18:03:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=v0yd.nl Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=v0yd.nl Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4T7Q644dvhz9srM; Sun, 7 Jan 2024 19:03:08 +0100 (CET) From: =?UTF-8?q?Jonas=20Dre=C3=9Fler?= To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Cc: =?UTF-8?q?Jonas=20Dre=C3=9Fler?= , asahi@lists.linux.dev, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3 4/4] Bluetooth: Disconnect connected devices before rfkilling adapter Date: Sun, 7 Jan 2024 19:02:50 +0100 Message-ID: <20240107180252.73436-5-verdre@v0yd.nl> In-Reply-To: <20240107180252.73436-1-verdre@v0yd.nl> References: <20240107180252.73436-1-verdre@v0yd.nl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a lot of platforms (at least the MS Surface devices, M1 macbooks, and a few ThinkPads) firmware doesn't do its job when rfkilling a device and the bluetooth adapter is not actually shut down properly on rfkill. This leads to connected devices remaining in connected state and the bluetooth connection eventually timing out after rfkilling an adapter. Use the rfkill hook in the HCI driver to go through the full power-off sequence (including stopping scans and disconnecting devices) before rfkilling it, just like MGMT_OP_SET_POWERED would do. In case anything during the larger power-off sequence fails, make sure the device is still closed and the rfkill ends up being effective in the end. Signed-off-by: Jonas Dreßler --- net/bluetooth/hci_core.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 1ec83985f..43e042338 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -940,20 +940,51 @@ int hci_get_dev_info(void __user *arg) /* ---- Interface to HCI drivers ---- */ +static int hci_dev_do_poweroff(struct hci_dev *hdev) +{ + int err; + + BT_DBG("%s %p", hdev->name, hdev); + + hci_req_sync_lock(hdev); + + err = hci_set_powered_sync(hdev, false); + + hci_req_sync_unlock(hdev); + + return err; +} + static int hci_rfkill_set_block(void *data, bool blocked) { struct hci_dev *hdev = data; + int err; BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) return -EBUSY; + if (blocked == hci_dev_test_flag(hdev, HCI_RFKILLED)) + return 0; + if (blocked) { hci_dev_set_flag(hdev, HCI_RFKILLED); + if (!hci_dev_test_flag(hdev, HCI_SETUP) && - !hci_dev_test_flag(hdev, HCI_CONFIG)) - hci_dev_do_close(hdev); + !hci_dev_test_flag(hdev, HCI_CONFIG)) { + err = hci_dev_do_poweroff(hdev); + if (err) { + bt_dev_err(hdev, "Error when powering off device on rfkill (%d)", + err); + + /* Make sure the device is still closed even if + * anything during power off sequence (eg. + * disconnecting devices) failed. + */ + hci_dev_do_close(hdev); + } + } } else { hci_dev_clear_flag(hdev, HCI_RFKILLED); } -- 2.43.0