Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp283552ybg; Thu, 17 Oct 2019 23:03:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqyWqVKovzyCyGR2bHdbUzOwhPO1ugGxg5x/T6/EhE9ZfWbSQ0FP0eHVg0N1uR0fZ+Y0pqqS X-Received: by 2002:aa7:d8d5:: with SMTP id k21mr4850366eds.286.1571378593777; Thu, 17 Oct 2019 23:03:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571378593; cv=none; d=google.com; s=arc-20160816; b=c090tY6874SE0XuDhT6AanqkBd4WK11j93mgjoVNrKPZ92gBn8cDKXabJRP9BHXQxx r9ZNy10TCksII4bkce4Sq3Qc/ZSO1QMc7aG1emKHeSZN0bOhwh1RzofQJbEtMQlMdFDm BZz9HGMiY0U8O4l1sQuH/hLYWFafF4Jxq3VzG0oTvYS5psdr63ibahRD8GAUc+NzEEy9 f4YID6lmf2outAE0ZvbqhxNASk8xPxoS1J0sJmxcQYILFVwVnrgLHFwP2Z9zFvbDzvTJ Vm8i5LNLX4OQuOoFKQjZnQVDqaw+DZ/HagGoiuNtnlSaDmw98PrOqOLBvKIulVFEx4++ 4zbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PR/rD0n5aMrsObQiFy5a+xTeVwlHVbH4Nmm3Ko6SbmQ=; b=GfOP30Ktsd/NXllfUpq0QxjMRphwGY5JdKGIYWRyI8JiDygrUDsZAfHRNN98HhgdMt ZD/tcVcQlXI2G5q5sZ5SGQbp2KmwYLnmpGHffPZ0EplOSyjSfN/BV5Qlv1bStfD30d0K lzzHvWRmXnRK1AJnHX18IZPfUKKFD7Mc++Kyp8lrSd1mYY/mp8bQQe0+JEJjeDZQXIcU Xr1WYZI7GUzx0VFqx1yd3Oh6gcyQ7K9Ebdt/T+Av9HtxInEsjATLs3nhBSiLnUfeUB33 jrpg0rOgoB+xDcRKQPQJGCBfqntOonDyTeIFeGTeqMY69dj/+XHBunzyMTVwRtynuFvt cXfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=GhOWxGUw; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h90si3255712edd.178.2019.10.17.23.02.46; Thu, 17 Oct 2019 23:03:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=GhOWxGUw; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391873AbfJQDVA (ORCPT + 99 others); Wed, 16 Oct 2019 23:21:00 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:45771 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730479AbfJQDVA (ORCPT ); Wed, 16 Oct 2019 23:21:00 -0400 Received: by mail-pl1-f194.google.com with SMTP id u12so396267pls.12 for ; Wed, 16 Oct 2019 20:20:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PR/rD0n5aMrsObQiFy5a+xTeVwlHVbH4Nmm3Ko6SbmQ=; b=GhOWxGUwokCNAw//y2NbGAiY+HvO2NijHZBU/ekO2lSz9uIbJaWbHZ966WunT4orRa cKkdhvmd3gm0bms/NTx/mX9CWbkFUpJ8yF/35v7SDPaNUq8tCQkiYa2UQDzQfVLAp8Fn p027HUgck/Vu0zhB43Flvpeil9BhDFFNfd/CcnWAkGCses8tF9y/F08yxn8Vig4Mkhw4 Vq4BkzrmHvOFVsMlhEQpsoXu30CPlop3NHV2szOnAy586QGIWQwQG/Akunh5MMtObkKL djsFCNbjpC3zhBAkyDzFrqjlZLTO/L3SVU5a7RZggMDZVYqiudPRWKeOFhdfdISWdJ8C OBUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PR/rD0n5aMrsObQiFy5a+xTeVwlHVbH4Nmm3Ko6SbmQ=; b=b0VAERN9DfllWru/VK8/q8yWbUVwI0zVblbBqKde8/gDTqvGzz0Ap88PcITFuQ/wvQ yydpy6oPUtSFCXAneFdaaGqPHadh07DkPaGVZKgd9HQNomxBIitcX9eAGAQnVBPOc2hH jJvAsnrfwzzjrfTokW+4/l17ZydXgcEiSqLl1L4zsrTZBWb6l65+RpNrqBbK9eP4JaCa LwjI+WShY3moOacaKmsXB5HvVhBD0RAMaUw8liK29zaEVExbVluC/vqDI0Y5/4AEWpQs UWis2N+hcMi+rNxQLMYnSyDrQ3Tu91KI5rxowPk9efC2zqvhc+2/+91mEe1cQAbknabt 8ZgA== X-Gm-Message-State: APjAAAXR67KUFRlfhO46wRQSJnBm2JFnJ5ekaDYy6bgpD+V2q4vpdo9Z eWJ8o+jhlQjZHxWrHf2PF8EhMLxJPw4= X-Received: by 2002:a17:902:321:: with SMTP id 30mr1618384pld.61.1571282459238; Wed, 16 Oct 2019 20:20:59 -0700 (PDT) Received: from mkorpershoek-XPS-13-9370.hsd1.ca.comcast.net ([2601:647:5700:f97e:1021:e5a8:28ca:ce5f]) by smtp.gmail.com with ESMTPSA id w12sm595664pfq.138.2019.10.16.20.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2019 20:20:58 -0700 (PDT) From: Mattijs Korpershoek To: linux-bluetooth@vger.kernel.org Cc: Mattijs Korpershoek , Marcel Holtmann , Johan Hedberg , "David S. Miller" , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] Bluetooth: hci_core: fix init for HCI_USER_CHANNEL Date: Wed, 16 Oct 2019 20:20:39 -0700 Message-Id: <20191017032039.18413-1-mkorpershoek@baylibre.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <474814D3-A97F-48D1-8268-3D200BE60795@holtmann.org> References: <474814D3-A97F-48D1-8268-3D200BE60795@holtmann.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org During the setup() stage, HCI device drivers expect the chip to acknowledge its setup() completion via vendor specific frames. If userspace opens() such HCI device in HCI_USER_CHANNEL [1] mode, the vendor specific frames are never tranmitted to the driver, as they are filtered in hci_rx_work(). Allow HCI devices which operate in HCI_USER_CHANNEL mode to receive frames if the HCI device is is HCI_INIT state. [1] https://www.spinics.net/lists/linux-bluetooth/msg37345.html Fixes: 23500189d7e0 ("Bluetooth: Introduce new HCI socket channel for user operation") Signed-off-by: Mattijs Korpershoek --- Changelog: v2: * change test logic to transfer packets when in INIT phase for user channel mode as recommended by Marcel * renamed patch from "Bluetooth: hci_core: fix init with HCI_QUIRK_NON_PERSISTENT_SETUP" v1: * https://lkml.org/lkml/2019/10/3/2250 Some more background on the change follows: The Android bluetooth stack (Bluedroid) also has a HAL implementation which follows Linux's standard rfkill interface [1]. This implementation relies on the HCI_CHANNEL_USER feature to get exclusive access to the underlying bluetooth device. When testing this along with the btkmtksdio driver, the chip appeared unresponsive when calling the following from userspace: struct sockaddr_hci addr; int fd; fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); memset(&addr, 0, sizeof(addr)); addr.hci_family = AF_BLUETOOTH; addr.hci_dev = 0; addr.hci_channel = HCI_CHANNEL_USER; bind(fd, (struct sockaddr *) &addr, sizeof(addr)); # device hangs In the case of bluetooth drivers exposing QUIRK_NON_PERSISTENT_SETUP such as btmtksdio, setup() is called each multiple times. In particular, when userspace calls bind(), the setup() is called again and vendor specific commands might be send to re-initialize the chip. Those commands are filtered out by hci_core in HCI_CHANNEL_USER mode, preventing setup() from completing successfully. This has been tested on a 4.19 kernel based on Android Common Kernel. It has also been compile tested on bluetooth-next. [1] https://android.googlesource.com/platform/system/bt/+/refs/heads/master/vendor_libs/linux/interface/ net/bluetooth/hci_core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index b2559d4bed81..0cc9ce917222 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -4440,7 +4440,14 @@ static void hci_rx_work(struct work_struct *work) hci_send_to_sock(hdev, skb); } - if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { + /* If the device has been opened in HCI_USER_CHANNEL, + * the userspace has exclusive access to device. + * When device is HCI_INIT, we still need to process + * the data packets to the driver in order + * to complete its setup(). + */ + if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && + !test_bit(HCI_INIT, &hdev->flags)) { kfree_skb(skb); continue; } -- 2.20.1