Path: blob/main/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c
35065 views
/*1* ng_ubt_rtl.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2019 Vladimir Kondratyev <[email protected]>8* Copyright (c) 2023 Future Crew LLC.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132/*33* Attempt to initialize FreeBSD bluetooth stack while Realtek 87XX/88XX USB34* device is in bootloader mode locks the adapter hardly so it requires power35* on/off cycle to restore. This driver blocks ng_ubt attachment until36* operational firmware is loaded by rtlbtfw utility thus avoiding the lock up.37*/3839#include <sys/types.h>40#include <sys/bus.h>41#include <sys/kernel.h>42#include <sys/mbuf.h>43#include <sys/module.h>44#include <sys/systm.h>45#include <sys/taskqueue.h>4647#include "usbdevs.h"48#include <dev/usb/usb.h>49#include <dev/usb/usbdi.h>50#define USB_DEBUG_VAR usb_debug51#include <dev/usb/usb_debug.h>5253#include <netgraph/ng_message.h>54#include <netgraph/netgraph.h>55#include <netgraph/ng_parse.h>56#include <netgraph/bluetooth/include/ng_bluetooth.h>57#include <netgraph/bluetooth/include/ng_hci.h>58#include <netgraph/bluetooth/include/ng_ubt.h>59#include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h>6061static device_probe_t ubt_rtl_probe;6263/*64* List of supported bluetooth devices. If you add a new device PID here ensure65* that it is blacklisted in ng_ubt.c and is supported by rtlbtfw utility.66*/6768const STRUCT_USB_HOST_ID ubt_rtl_devs[] =69{70/* Generic Realtek Bluetooth class devices */71{ USB_VENDOR(USB_VENDOR_REALTEK),72USB_IFACE_CLASS(UDCLASS_WIRELESS),73USB_IFACE_SUBCLASS(UDSUBCLASS_RF),74USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) },7576/* Realtek 8821CE Bluetooth devices */77{ USB_VPI(0x13d3, 0x3529, 0) },7879/* Realtek 8822CE Bluetooth devices */80{ USB_VPI(0x0bda, 0xb00c, 0) },81{ USB_VPI(0x0bda, 0xc822, 0) },8283/* Realtek 8822CU Bluetooth devices */84{ USB_VPI(0x13d3, 0x3549, 0) },8586/* Realtek 8851BE Bluetooth devices */87{ USB_VPI(0x13d3, 0x3600, 0) },8889/* Realtek 8852AE Bluetooth devices */90{ USB_VPI(0x0bda, 0x2852, 0) },91{ USB_VPI(0x0bda, 0xc852, 0) },92{ USB_VPI(0x0bda, 0x385a, 0) },93{ USB_VPI(0x0bda, 0x4852, 0) },94{ USB_VPI(0x04c5, 0x165c, 0) },95{ USB_VPI(0x04ca, 0x4006, 0) },96{ USB_VPI(0x0cb8, 0xc549, 0) },9798/* Realtek 8852CE Bluetooth devices */99{ USB_VPI(0x04ca, 0x4007, 0) },100{ USB_VPI(0x04c5, 0x1675, 0) },101{ USB_VPI(0x0cb8, 0xc558, 0) },102{ USB_VPI(0x13d3, 0x3587, 0) },103{ USB_VPI(0x13d3, 0x3586, 0) },104{ USB_VPI(0x13d3, 0x3592, 0) },105{ USB_VPI(0x0489, 0xe122, 0) },106107/* Realtek 8852BE Bluetooth devices */108{ USB_VPI(0x0cb8, 0xc559, 0) },109{ USB_VPI(0x0bda, 0x4853, 0) },110{ USB_VPI(0x0bda, 0x887b, 0) },111{ USB_VPI(0x0bda, 0xb85b, 0) },112{ USB_VPI(0x13d3, 0x3570, 0) },113{ USB_VPI(0x13d3, 0x3571, 0) },114{ USB_VPI(0x13d3, 0x3572, 0) },115{ USB_VPI(0x13d3, 0x3591, 0) },116{ USB_VPI(0x0489, 0xe123, 0) },117{ USB_VPI(0x0489, 0xe125, 0) },118119/* Realtek 8852BT/8852BE-VT Bluetooth devices */120{ USB_VPI(0x0bda, 0x8520, 0) },121122/* Realtek 8922AE Bluetooth devices */123{ USB_VPI(0x0bda, 0x8922, 0) },124{ USB_VPI(0x13d3, 0x3617, 0) },125{ USB_VPI(0x13d3, 0x3616, 0) },126{ USB_VPI(0x0489, 0xe130, 0) },127128/* Realtek 8723AE Bluetooth devices */129{ USB_VPI(0x0930, 0x021d, 0) },130{ USB_VPI(0x13d3, 0x3394, 0) },131132/* Realtek 8723BE Bluetooth devices */133{ USB_VPI(0x0489, 0xe085, 0) },134{ USB_VPI(0x0489, 0xe08b, 0) },135{ USB_VPI(0x04f2, 0xb49f, 0) },136{ USB_VPI(0x13d3, 0x3410, 0) },137{ USB_VPI(0x13d3, 0x3416, 0) },138{ USB_VPI(0x13d3, 0x3459, 0) },139{ USB_VPI(0x13d3, 0x3494, 0) },140141/* Realtek 8723BU Bluetooth devices */142{ USB_VPI(0x7392, 0xa611, 0) },143144/* Realtek 8723DE Bluetooth devices */145{ USB_VPI(0x0bda, 0xb009, 0) },146{ USB_VPI(0x2ff8, 0xb011, 0) },147148/* Realtek 8761BUV Bluetooth devices */149{ USB_VPI(0x2c4e, 0x0115, 0) },150{ USB_VPI(0x2357, 0x0604, 0) },151{ USB_VPI(0x0b05, 0x190e, 0) },152{ USB_VPI(0x2550, 0x8761, 0) },153{ USB_VPI(0x0bda, 0x8771, 0) },154{ USB_VPI(0x6655, 0x8771, 0) },155{ USB_VPI(0x7392, 0xc611, 0) },156{ USB_VPI(0x2b89, 0x8761, 0) },157158/* Realtek 8821AE Bluetooth devices */159{ USB_VPI(0x0b05, 0x17dc, 0) },160{ USB_VPI(0x13d3, 0x3414, 0) },161{ USB_VPI(0x13d3, 0x3458, 0) },162{ USB_VPI(0x13d3, 0x3461, 0) },163{ USB_VPI(0x13d3, 0x3462, 0) },164165/* Realtek 8822BE Bluetooth devices */166{ USB_VPI(0x13d3, 0x3526, 0) },167{ USB_VPI(0x0b05, 0x185c, 0) },168169/* Realtek 8822CE Bluetooth devices */170{ USB_VPI(0x04ca, 0x4005, 0) },171{ USB_VPI(0x04c5, 0x161f, 0) },172{ USB_VPI(0x0b05, 0x18ef, 0) },173{ USB_VPI(0x13d3, 0x3548, 0) },174{ USB_VPI(0x13d3, 0x3549, 0) },175{ USB_VPI(0x13d3, 0x3553, 0) },176{ USB_VPI(0x13d3, 0x3555, 0) },177{ USB_VPI(0x2ff8, 0x3051, 0) },178{ USB_VPI(0x1358, 0xc123, 0) },179{ USB_VPI(0x0bda, 0xc123, 0) },180{ USB_VPI(0x0cb5, 0xc547, 0) },181};182const size_t ubt_rtl_devs_sizeof = sizeof(ubt_rtl_devs);183184/*185* List of lmp_subversion values that correspond to Realtek firmwares186* incompatible with ng_ubt driver. Alternative firmware for these devices187* has to be loaded with rtlbtfw utility. That will trigger lmp_subversion188* change to different value.189*/190static const uint16_t ubt_rtl_lmp_subvers[] = {1910x8703, 0x1200, 0x8723, 0x8821,1920x8761, 0x8822, 0x8852, 0x8851,193};194195/*196* Execute generic HCI command and return response in provided buffer.197*/198199static usb_error_t200ubt_rtl_do_hci_request(struct usb_device *udev, uint16_t opcode,201void *resp, uint8_t resp_len)202{203#define UBT_RTL_HCICMD_TIMEOUT 2000 /* ms */204struct ubt_hci_event_command_compl *evt;205struct ubt_hci_cmd cmd;206usb_error_t error;207208memset(&cmd, 0, sizeof(cmd));209cmd.opcode = htole16(opcode);210evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) +211resp_len, M_TEMP, M_ZERO | M_WAITOK);212evt->header.event = NG_HCI_EVENT_COMMAND_COMPL;213evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE;214215error = ubt_do_hci_request(udev, &cmd, evt, UBT_RTL_HCICMD_TIMEOUT);216if (error != USB_ERR_NORMAL_COMPLETION)217goto exit;218219if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL &&220evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE)221memcpy(resp, evt->data, resp_len);222else223error = USB_ERR_INVAL;224exit:225free(evt, M_TEMP);226return (error);227}228229/*230* Probe for a Realtek 87XX/88XX USB Bluetooth device.231*/232233static int234ubt_rtl_probe(device_t dev)235{236struct usb_attach_arg *uaa = device_get_ivars(dev);237ng_hci_read_local_ver_rp ver;238unsigned int i;239int error;240241if (uaa->usb_mode != USB_MODE_HOST)242return (ENXIO);243244if (uaa->info.bIfaceIndex != 0)245return (ENXIO);246247error = usbd_lookup_id_by_uaa(ubt_rtl_devs, sizeof(ubt_rtl_devs), uaa);248if (error != 0)249return (error);250251if (ubt_rtl_do_hci_request(uaa->device,252NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER),253&ver, sizeof(ver)) != USB_ERR_NORMAL_COMPLETION)254return (ENXIO);255256DPRINTFN(2, "hci_version 0x%02x\n", ver.hci_version);257DPRINTFN(2, "hci_revision 0x%04x\n", le16toh(ver.hci_revision));258DPRINTFN(2, "lmp_version 0x%02x\n", ver.lmp_version);259DPRINTFN(2, "lmp_subversion 0x%04x\n", le16toh(ver.lmp_subversion));260261for (i = 0; i < nitems(ubt_rtl_lmp_subvers); i++)262if (le16toh(ver.lmp_subversion) == ubt_rtl_lmp_subvers[i])263return (ENXIO);264265return (BUS_PROBE_DEFAULT);266}267268/*269* Module interface. Attach and detach methods, netgraph node type270* registration and PNP string are inherited from ng_ubt.c driver.271*/272273static device_method_t ubt_rtl_methods[] =274{275DEVMETHOD(device_probe, ubt_rtl_probe),276DEVMETHOD_END277};278279DEFINE_CLASS_1(ubt, ubt_rtl_driver, ubt_rtl_methods,280sizeof(struct ubt_softc), ubt_driver);281DRIVER_MODULE(ng_ubt_rtl, uhub, ubt_rtl_driver, 0, 0);282MODULE_VERSION(ng_ubt_rtl, NG_BLUETOOTH_VERSION);283MODULE_DEPEND(ng_ubt_rtl, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);284MODULE_DEPEND(ng_ubt_rtl, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);285MODULE_DEPEND(ng_ubt_rtl, usb, 1, 1, 1);286287288