From patchwork Sun Jan 7 18:02:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Jonas_Dre=C3=9Fler?= X-Patchwork-Id: 185755 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp646677dyq; Sun, 7 Jan 2024 10:04:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IGSOeCq9ET3B0LvrGFSlKVq03pbyy71zeMwFsedFIiucvuAO6rSYHq02qaX1js3m2b0Eepd X-Received: by 2002:a05:622a:15c1:b0:427:a420:b1f with SMTP id d1-20020a05622a15c100b00427a4200b1fmr4159829qty.13.1704650696770; Sun, 07 Jan 2024 10:04:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704650696; cv=none; d=google.com; s=arc-20160816; b=G2BPkwPYO2ZU27SE7QkCNU67HLmfR25eM30aOTcTH6Rfqv2FSGPq0+eHwshQwklT+5 gOp73UpAbQn7ISVnr55oHEI+ozk8jUlOB3CRSpU7iJenRu1a+HY7FkREqyCjoKL2EYB5 1qYPDcm1yTqY8ytYiXdjARv0K5XmouFPB2JYjG1FezhMFQ8LLExBgbw7lbEnqOdHL/yZ PgdYXNjLTynpdphesW9TYbqNRBUq7PA+YHh9tDbY11hiEy7DvZoWN2UA+ovrbQFJ5h9M PdYo9hrdcxPcersNiUetoOFNdbgl8GyQcTLHZXg3/i3KqHDNvacmZ8tAvGMrx4oVBm6t m/Mw== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18944-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id r9-20020a05622a034900b004297e8fe7b2si4171598qtw.530.2024.01.07.10.04.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jan 2024 10:04:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18944-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18944-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18944-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E76F21C216A9 for ; Sun, 7 Jan 2024 18:04:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8BC7220323; Sun, 7 Jan 2024 18:03:20 +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_Dre=C3=9Fler?= To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Cc: =?utf-8?q?Jonas_Dre=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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787455809289923822 X-GMAIL-MSGID: 1787455809289923822 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); }