From nobody Wed Nov 06 23:31:56 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XkM184ySGz5c0YW; Wed, 06 Nov 2024 23:31:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XkM182ntJz4Ltl; Wed, 6 Nov 2024 23:31:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730935916; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jqqnU7r2k7eq+nRcZf3oyuxECbjLyL9n5c8zcYXBnPw=; b=aJF7dAJevkMJeZ4vhvaz8g1PBcQZfZuGUIGwhdCIMA+SKkFMfeROf9tk8FvQ6baQ+VtbMU eAV12GueLCmrcFicaSSOOKxAKVtp/TMafMkVwAdhbD6+De6ng0i7WtNi+VT78GJl1Y0wYu CnSyOWEqElvfZt0Nchp57S7yqBvieKbUCAifkDaY2Ahv/ypsSGFKXqREbhDAhas8f/Ps2r xcNQV7IvCQya9bmtYtqN/CPz6T8TVg9BOhk9ZW/j1kQYZgQPzGs80vZ5L/eEfIeHh6BUiN kl4iFB4Uj4LjPMZjft234FyytkwPQqakWmXBmPZGbP90fGpylVNUIrvifq6Y/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730935916; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jqqnU7r2k7eq+nRcZf3oyuxECbjLyL9n5c8zcYXBnPw=; b=ClV3NOp+y0AtM8N5dQ2uptoPWSJVOIhSc30Ft+eWuq/5e4ZHaRfocLn/iPhXeYk2EcI8Wt 83RfV6ZIe/nJw/Z4TB28LLPiB+qLFg+ciKlxp3CtqJMTx/dj0542RXVh4f1yK6TKlaM/AE ARfo0zwD48pcZi9CCRh3Hcoe/PNXhmY0Ykp2BsNCR6O2iutpOUDl3i2UOpC4euPyuBfJ/d Cu27Jq/IH9odwWbr4Vy5XxTwr7IguKxQjMzAg46nmpGm27rAvEa6WiLizB5KEhtkwWKu17 zMXRKvs9VcF5lUX9GCQ0eoi3RqSD5/IWVIvlUScbaRqxhc9fSREz80LNcBT3fA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1730935916; a=rsa-sha256; cv=none; b=E2MZyPFxoApOe8H3+YLh1YOS3TYLahZ9Ckkd98tfo6/ulDh6e6eUe086n4avhWO3iHrHmN NVUDpi5H4utVQLJ1gjFz7ckWyjYhNXdPBCZbAGGfI2uBeCT2t80cIWdCMm7McInumMSx6g hml+iyfZQUtcDGCNn5LkCdL9wngXZcMvVfKDxu1hbvW1cHqs6EnUfLQQNJrdgdtsYItrhT A3TK1vjIoSMzZ9/jZOjmW4HPhBS87iGZdalHHNsv6LeU0xb03tP6RS5N8yQyFzQV7Ux/ss 8i4KHGiNMon/5m3wLxEbSD50SbeiTHMSyMwG9/Uo2LtYEn+/dc0PNoEIY4FbBQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XkM182PF3zLs8; Wed, 6 Nov 2024 23:31:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4A6NVueo070021; Wed, 6 Nov 2024 23:31:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4A6NVuiE070018; Wed, 6 Nov 2024 23:31:56 GMT (envelope-from git) Date: Wed, 6 Nov 2024 23:31:56 GMT Message-Id: <202411062331.4A6NVuiE070018@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Vladimir Kondratyev Subject: git: 24ae172a5035 - main - ng_ubt(4): do not attach Realtek 87XX/88XX adaptors in bootloader mode. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wulf X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 24ae172a50352ad4fd22989477f29ecca5aed6e3 Auto-Submitted: auto-generated The branch main has been updated by wulf: URL: https://cgit.FreeBSD.org/src/commit/?id=24ae172a50352ad4fd22989477f29ecca5aed6e3 commit 24ae172a50352ad4fd22989477f29ecca5aed6e3 Author: Vladimir Kondratyev AuthorDate: 2024-11-06 23:28:11 +0000 Commit: Vladimir Kondratyev CommitDate: 2024-11-06 23:28:11 +0000 ng_ubt(4): do not attach Realtek 87XX/88XX adaptors in bootloader mode. Attempt to initialize FreeBSD bluetooth stack while such a device is in bootloader mode locks the adapter hardly so it requires power on/off cycle to restore. This change blocks ng_ubt attachment unless operational firmware is loaded thus preventing the lock up. Sponsored by: Future Crew LLC MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D46738 --- sys/conf/files | 1 + sys/modules/netgraph/bluetooth/ubt/Makefile | 4 +- sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c | 3 + sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c | 265 ++++++++++++++++++++++++ sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h | 3 + 5 files changed, 274 insertions(+), 2 deletions(-) diff --git a/sys/conf/files b/sys/conf/files index 7bf2cffe8b09..7818104b8d50 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -4267,6 +4267,7 @@ net80211/ieee80211_alq.c optional wlan ieee80211_alq netgraph/bluetooth/common/ng_bluetooth.c optional netgraph_bluetooth netgraph/bluetooth/drivers/ubt/ng_ubt.c optional netgraph_bluetooth_ubt usb netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c optional netgraph_bluetooth_ubt usb +netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c optional netgraph_bluetooth_ubt usb netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c optional netgraph_bluetooth_ubtbcmfw usb netgraph/bluetooth/hci/ng_hci_cmds.c optional netgraph_bluetooth_hci netgraph/bluetooth/hci/ng_hci_evnt.c optional netgraph_bluetooth_hci diff --git a/sys/modules/netgraph/bluetooth/ubt/Makefile b/sys/modules/netgraph/bluetooth/ubt/Makefile index 6ef7da5fbfb2..1d95fd567888 100644 --- a/sys/modules/netgraph/bluetooth/ubt/Makefile +++ b/sys/modules/netgraph/bluetooth/ubt/Makefile @@ -6,7 +6,7 @@ CFLAGS+= -I${SRCTOP}/sys/netgraph/bluetooth/include \ -I${SRCTOP}/sys/netgraph/bluetooth/drivers/ubt KMOD= ng_ubt -SRCS= ng_ubt.c ng_ubt_intel.c opt_bus.h opt_usb.h device_if.h \ - bus_if.h usb_if.h usbdevs.h +SRCS= ng_ubt.c ng_ubt_intel.c ng_ubt_rtl.c opt_bus.h opt_usb.h \ + device_if.h bus_if.h usb_if.h usbdevs.h .include diff --git a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c index 59405629efb1..def589107725 100644 --- a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c +++ b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c @@ -625,6 +625,9 @@ ubt_probe(device_t dev) if (usbd_lookup_id_by_uaa(ubt_ignore_devs, sizeof(ubt_ignore_devs), uaa) == 0) return (ENXIO); + if (usbd_lookup_id_by_uaa(ubt_rtl_devs, + ubt_rtl_devs_sizeof, uaa) == 0) + return (ENXIO); id = usbd_lookup_id_by_info(ubt_devs, sizeof(ubt_devs), &uaa->info); diff --git a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c new file mode 100644 index 000000000000..b9fbc1c12a29 --- /dev/null +++ b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c @@ -0,0 +1,265 @@ +/* + * ng_ubt_rtl.c + */ + +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Vladimir Kondratyev + * Copyright (c) 2023 Future Crew LLC. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Attempt to initialize FreeBSD bluetooth stack while Realtek 87XX/88XX USB + * device is in bootloader mode locks the adapter hardly so it requires power + * on/off cycle to restore. This driver blocks ng_ubt attachment until + * operational firmware is loaded by rtlbtfw utility thus avoiding the lock up. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "usbdevs.h" +#include +#include +#define USB_DEBUG_VAR usb_debug +#include + +#include +#include +#include +#include +#include +#include +#include + +static device_probe_t ubt_rtl_probe; + +/* + * List of supported bluetooth devices. If you add a new device PID here ensure + * that it is blacklisted in ng_ubt.c and is supported by rtlbtfw utility. + */ + +const STRUCT_USB_HOST_ID ubt_rtl_devs[] = +{ + /* Generic Realtek Bluetooth class devices */ + { USB_VENDOR(USB_VENDOR_REALTEK), + USB_IFACE_CLASS(UDCLASS_WIRELESS), + USB_IFACE_SUBCLASS(UDSUBCLASS_RF), + USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, + + /* Realtek 8821CE Bluetooth devices */ + { USB_VPI(0x13d3, 0x3529, 0) }, + + /* Realtek 8822CE Bluetooth devices */ + { USB_VPI(0x0bda, 0xb00c, 0) }, + { USB_VPI(0x0bda, 0xc822, 0) }, + + /* Realtek 8822CU Bluetooth devices */ + { USB_VPI(0x13d3, 0x3549, 0) }, + + /* Realtek 8852AE Bluetooth devices */ + { USB_VPI(0x0bda, 0x2852, 0) }, + { USB_VPI(0x0bda, 0xc852, 0) }, + { USB_VPI(0x0bda, 0x385a, 0) }, + { USB_VPI(0x0bda, 0x4852, 0) }, + { USB_VPI(0x04c5, 0x165c, 0) }, + { USB_VPI(0x04ca, 0x4006, 0) }, + { USB_VPI(0x0cb8, 0xc549, 0) }, + + /* Realtek 8852CE Bluetooth devices */ + { USB_VPI(0x04ca, 0x4007, 0) }, + { USB_VPI(0x04c5, 0x1675, 0) }, + { USB_VPI(0x0cb8, 0xc558, 0) }, + { USB_VPI(0x13d3, 0x3587, 0) }, + { USB_VPI(0x13d3, 0x3586, 0) }, + { USB_VPI(0x13d3, 0x3592, 0) }, + + /* Realtek 8852BE Bluetooth devices */ + { USB_VPI(0x0cb8, 0xc559, 0) }, + { USB_VPI(0x0bda, 0x887b, 0) }, + { USB_VPI(0x13d3, 0x3571, 0) }, + + /* Realtek 8723AE Bluetooth devices */ + { USB_VPI(0x0930, 0x021d, 0) }, + { USB_VPI(0x13d3, 0x3394, 0) }, + + /* Realtek 8723BE Bluetooth devices */ + { USB_VPI(0x0489, 0xe085, 0) }, + { USB_VPI(0x0489, 0xe08b, 0) }, + { USB_VPI(0x04f2, 0xb49f, 0) }, + { USB_VPI(0x13d3, 0x3410, 0) }, + { USB_VPI(0x13d3, 0x3416, 0) }, + { USB_VPI(0x13d3, 0x3459, 0) }, + { USB_VPI(0x13d3, 0x3494, 0) }, + + /* Realtek 8723BU Bluetooth devices */ + { USB_VPI(0x7392, 0xa611, 0) }, + + /* Realtek 8723DE Bluetooth devices */ + { USB_VPI(0x0bda, 0xb009, 0) }, + { USB_VPI(0x2ff8, 0xb011, 0) }, + + /* Realtek 8761BUV Bluetooth devices */ + { USB_VPI(0x2357, 0x0604, 0) }, + { USB_VPI(0x0b05, 0x190e, 0) }, + { USB_VPI(0x2550, 0x8761, 0) }, + { USB_VPI(0x0bda, 0x8771, 0) }, + { USB_VPI(0x6655, 0x8771, 0) }, + { USB_VPI(0x7392, 0xc611, 0) }, + { USB_VPI(0x2b89, 0x8761, 0) }, + + /* Realtek 8821AE Bluetooth devices */ + { USB_VPI(0x0b05, 0x17dc, 0) }, + { USB_VPI(0x13d3, 0x3414, 0) }, + { USB_VPI(0x13d3, 0x3458, 0) }, + { USB_VPI(0x13d3, 0x3461, 0) }, + { USB_VPI(0x13d3, 0x3462, 0) }, + + /* Realtek 8822BE Bluetooth devices */ + { USB_VPI(0x13d3, 0x3526, 0) }, + { USB_VPI(0x0b05, 0x185c, 0) }, + + /* Realtek 8822CE Bluetooth devices */ + { USB_VPI(0x04ca, 0x4005, 0) }, + { USB_VPI(0x04c5, 0x161f, 0) }, + { USB_VPI(0x0b05, 0x18ef, 0) }, + { USB_VPI(0x13d3, 0x3548, 0) }, + { USB_VPI(0x13d3, 0x3549, 0) }, + { USB_VPI(0x13d3, 0x3553, 0) }, + { USB_VPI(0x13d3, 0x3555, 0) }, + { USB_VPI(0x2ff8, 0x3051, 0) }, + { USB_VPI(0x1358, 0xc123, 0) }, + { USB_VPI(0x0bda, 0xc123, 0) }, + { USB_VPI(0x0cb5, 0xc547, 0) }, +}; +const size_t ubt_rtl_devs_sizeof = sizeof(ubt_rtl_devs); + +/* + * List of lmp_subversion values that correspond to Realtek firmwares + * incompatible with ng_ubt driver. Alternative firmware for these devices + * has to be loaded with rtlbtfw utility. That will trigger lmp_subversion + * change to different value. + */ +static const uint16_t ubt_rtl_lmp_subvers[] = { + 0x8703, 0x1200, 0x8723, 0x8821, + 0x8761, 0x8822, 0x8852, 0x8851, +}; + +/* + * Execute generic HCI command and return response in provided buffer. + */ + +static usb_error_t +ubt_rtl_do_hci_request(struct usb_device *udev, uint16_t opcode, + void *resp, uint8_t resp_len) +{ +#define UBT_RTL_HCICMD_TIMEOUT 2000 /* ms */ + struct ubt_hci_event_command_compl *evt; + struct ubt_hci_cmd cmd; + usb_error_t error; + + memset(&cmd, 0, sizeof(cmd)); + cmd.opcode = htole16(opcode); + evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) + + resp_len, M_TEMP, M_ZERO | M_WAITOK); + evt->header.event = NG_HCI_EVENT_COMMAND_COMPL; + evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE; + + error = ubt_do_hci_request(udev, &cmd, evt, UBT_RTL_HCICMD_TIMEOUT); + if (error != USB_ERR_NORMAL_COMPLETION) + goto exit; + + if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL && + evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE) + memcpy(resp, evt->data, resp_len); + else + error = USB_ERR_INVAL; +exit: + free(evt, M_TEMP); + return (error); +} + +/* + * Probe for a Realtek 87XX/88XX USB Bluetooth device. + */ + +static int +ubt_rtl_probe(device_t dev) +{ + struct usb_attach_arg *uaa = device_get_ivars(dev); + ng_hci_read_local_ver_rp ver; + unsigned int i; + int error; + + if (uaa->usb_mode != USB_MODE_HOST) + return (ENXIO); + + if (uaa->info.bIfaceIndex != 0) + return (ENXIO); + + error = usbd_lookup_id_by_uaa(ubt_rtl_devs, sizeof(ubt_rtl_devs), uaa); + if (error != 0) + return (error); + + if (ubt_rtl_do_hci_request(uaa->device, + NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER), + &ver, sizeof(ver)) != USB_ERR_NORMAL_COMPLETION) + return (ENXIO); + + DPRINTFN(2, "hci_version 0x%02x\n", ver.hci_version); + DPRINTFN(2, "hci_revision 0x%04x\n", le16toh(ver.hci_revision)); + DPRINTFN(2, "lmp_version 0x%02x\n", ver.lmp_version); + DPRINTFN(2, "lmp_subversion 0x%04x\n", le16toh(ver.lmp_subversion)); + + for (i = 0; i < nitems(ubt_rtl_lmp_subvers); i++) + if (le16toh(ver.lmp_subversion) == ubt_rtl_lmp_subvers[i]) + return (ENXIO); + + return (BUS_PROBE_DEFAULT); +} + +/* + * Module interface. Attach and detach methods, netgraph node type + * registration and PNP string are inherited from ng_ubt.c driver. + */ + +static device_method_t ubt_rtl_methods[] = +{ + DEVMETHOD(device_probe, ubt_rtl_probe), + DEVMETHOD_END +}; + +DEFINE_CLASS_1(ubt, ubt_rtl_driver, ubt_rtl_methods, + sizeof(struct ubt_softc), ubt_driver); +DRIVER_MODULE(ng_ubt_rtl, uhub, ubt_rtl_driver, 0, 0); +MODULE_VERSION(ng_ubt_rtl, NG_BLUETOOTH_VERSION); +MODULE_DEPEND(ng_ubt_rtl, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); +MODULE_DEPEND(ng_ubt_rtl, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION); +MODULE_DEPEND(ng_ubt_rtl, usb, 1, 1, 1); diff --git a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h index 7b97ad245a68..a48a6732f5a8 100644 --- a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h +++ b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_var.h @@ -164,6 +164,9 @@ typedef struct ubt_softc * ubt_softc_p; usb_error_t ubt_do_hci_request(struct usb_device *, struct ubt_hci_cmd *, void *, usb_timeout_t); +extern const STRUCT_USB_HOST_ID ubt_rtl_devs[]; +extern const size_t ubt_rtl_devs_sizeof; + extern driver_t ubt_driver; #endif /* ndef _NG_UBT_VAR_H_ */