Path: blob/main/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c
108090 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 8851BE Bluetooth devices */84{ USB_VPI(0x13d3, 0x3600, 0) },8586/* Realtek 8852AE Bluetooth devices */87{ USB_VPI(0x0bda, 0x2852, 0) },88{ USB_VPI(0x0bda, 0xc852, 0) },89{ USB_VPI(0x0bda, 0x385a, 0) },90{ USB_VPI(0x0bda, 0x4852, 0) },91{ USB_VPI(0x04c5, 0x165c, 0) },92{ USB_VPI(0x04ca, 0x4006, 0) },93{ USB_VPI(0x0cb8, 0xc549, 0) },9495/* Realtek 8852CE Bluetooth devices */96{ USB_VPI(0x04ca, 0x4007, 0) },97{ USB_VPI(0x04c5, 0x1675, 0) },98{ USB_VPI(0x0cb8, 0xc558, 0) },99{ USB_VPI(0x13d3, 0x3587, 0) },100{ USB_VPI(0x13d3, 0x3586, 0) },101{ USB_VPI(0x13d3, 0x3592, 0) },102{ USB_VPI(0x0489, 0xe122, 0) },103104/* Realtek 8852BE Bluetooth devices */105{ USB_VPI(0x0cb8, 0xc559, 0) },106{ USB_VPI(0x0bda, 0x4853, 0) },107{ USB_VPI(0x0bda, 0x887b, 0) },108{ USB_VPI(0x0bda, 0xb85b, 0) },109{ USB_VPI(0x13d3, 0x3570, 0) },110{ USB_VPI(0x13d3, 0x3571, 0) },111{ USB_VPI(0x13d3, 0x3572, 0) },112{ USB_VPI(0x13d3, 0x3591, 0) },113{ USB_VPI(0x0489, 0xe123, 0) },114{ USB_VPI(0x0489, 0xe125, 0) },115116/* Realtek 8852BT/8852BE-VT Bluetooth devices */117{ USB_VPI(0x0bda, 0x8520, 0) },118119/* Realtek 8922AE Bluetooth devices */120{ USB_VPI(0x0bda, 0x8922, 0) },121{ USB_VPI(0x13d3, 0x3617, 0) },122{ USB_VPI(0x13d3, 0x3616, 0) },123{ USB_VPI(0x0489, 0xe130, 0) },124125/* Realtek 8723AE Bluetooth devices */126{ USB_VPI(0x0930, 0x021d, 0) },127{ USB_VPI(0x13d3, 0x3394, 0) },128129/* Realtek 8723BE Bluetooth devices */130{ USB_VPI(0x0489, 0xe085, 0) },131{ USB_VPI(0x0489, 0xe08b, 0) },132{ USB_VPI(0x04f2, 0xb49f, 0) },133{ USB_VPI(0x13d3, 0x3410, 0) },134{ USB_VPI(0x13d3, 0x3416, 0) },135{ USB_VPI(0x13d3, 0x3459, 0) },136{ USB_VPI(0x13d3, 0x3494, 0) },137138/* Realtek 8723BU Bluetooth devices */139{ USB_VPI(0x7392, 0xa611, 0) },140141/* Realtek 8723DE Bluetooth devices */142{ USB_VPI(0x0bda, 0xb009, 0) },143{ USB_VPI(0x2ff8, 0xb011, 0) },144145/* Realtek 8761BUV Bluetooth devices */146{ USB_VPI(0x2c4e, 0x0115, 0) },147{ USB_VPI(0x2357, 0x0604, 0) },148{ USB_VPI(0x0b05, 0x190e, 0) },149{ USB_VPI(0x2550, 0x8761, 0) },150{ USB_VPI(0x0bda, 0x8771, 0) },151{ USB_VPI(0x6655, 0x8771, 0) },152{ USB_VPI(0x7392, 0xc611, 0) },153{ USB_VPI(0x2b89, 0x8761, 0) },154155/* Realtek 8821AE Bluetooth devices */156{ USB_VPI(0x0b05, 0x17dc, 0) },157{ USB_VPI(0x13d3, 0x3414, 0) },158{ USB_VPI(0x13d3, 0x3458, 0) },159{ USB_VPI(0x13d3, 0x3461, 0) },160{ USB_VPI(0x13d3, 0x3462, 0) },161162/* Realtek 8822BE Bluetooth devices */163{ USB_VPI(0x13d3, 0x3526, 0) },164{ USB_VPI(0x0b05, 0x185c, 0) },165166/* Realtek 8822CE Bluetooth devices */167{ USB_VPI(0x04ca, 0x4005, 0) },168{ USB_VPI(0x04c5, 0x161f, 0) },169{ USB_VPI(0x0b05, 0x18ef, 0) },170{ USB_VPI(0x13d3, 0x3548, 0) },171{ USB_VPI(0x13d3, 0x3549, 0) },172{ USB_VPI(0x13d3, 0x3553, 0) },173{ USB_VPI(0x13d3, 0x3555, 0) },174{ USB_VPI(0x2ff8, 0x3051, 0) },175{ USB_VPI(0x1358, 0xc123, 0) },176{ USB_VPI(0x0bda, 0xc123, 0) },177{ USB_VPI(0x0cb5, 0xc547, 0) },178};179const size_t ubt_rtl_devs_sizeof = sizeof(ubt_rtl_devs);180181/*182* List of lmp_subversion values that correspond to Realtek firmwares183* incompatible with ng_ubt driver. Alternative firmware for these devices184* has to be loaded with rtlbtfw utility. That will trigger lmp_subversion185* change to different value.186*/187static const uint16_t ubt_rtl_lmp_subvers[] = {1880x8703, 0x1200, 0x8723, 0x8821,1890x8761, 0x8822, 0x8852, 0x8851,190};191192/*193* Execute generic HCI command and return response in provided buffer.194*/195196static usb_error_t197ubt_rtl_do_hci_request(struct usb_device *udev, uint16_t opcode,198void *resp, uint8_t resp_len)199{200#define UBT_RTL_HCICMD_TIMEOUT 2000 /* ms */201struct ubt_hci_event_command_compl *evt;202struct ubt_hci_cmd cmd;203usb_error_t error;204205memset(&cmd, 0, sizeof(cmd));206cmd.opcode = htole16(opcode);207evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) +208resp_len, M_TEMP, M_ZERO | M_WAITOK);209evt->header.event = NG_HCI_EVENT_COMMAND_COMPL;210evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE;211212error = ubt_do_hci_request(udev, &cmd, evt, UBT_RTL_HCICMD_TIMEOUT);213if (error != USB_ERR_NORMAL_COMPLETION)214goto exit;215216if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL &&217evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE)218memcpy(resp, evt->data, resp_len);219else220error = USB_ERR_INVAL;221exit:222free(evt, M_TEMP);223return (error);224}225226/*227* Probe for a Realtek 87XX/88XX USB Bluetooth device.228*/229230static int231ubt_rtl_probe(device_t dev)232{233struct usb_attach_arg *uaa = device_get_ivars(dev);234ng_hci_read_local_ver_rp ver;235unsigned int i;236int error;237238if (uaa->usb_mode != USB_MODE_HOST)239return (ENXIO);240241if (uaa->info.bIfaceIndex != 0)242return (ENXIO);243244error = usbd_lookup_id_by_uaa(ubt_rtl_devs, sizeof(ubt_rtl_devs), uaa);245if (error != 0)246return (error);247248if (ubt_rtl_do_hci_request(uaa->device,249NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER),250&ver, sizeof(ver)) != USB_ERR_NORMAL_COMPLETION)251return (ENXIO);252253DPRINTFN(2, "hci_version 0x%02x\n", ver.hci_version);254DPRINTFN(2, "hci_revision 0x%04x\n", le16toh(ver.hci_revision));255DPRINTFN(2, "lmp_version 0x%02x\n", ver.lmp_version);256DPRINTFN(2, "lmp_subversion 0x%04x\n", le16toh(ver.lmp_subversion));257258for (i = 0; i < nitems(ubt_rtl_lmp_subvers); i++)259if (le16toh(ver.lmp_subversion) == ubt_rtl_lmp_subvers[i])260return (ENXIO);261262return (BUS_PROBE_DEFAULT);263}264265/*266* Module interface. Attach and detach methods, netgraph node type267* registration and PNP string are inherited from ng_ubt.c driver.268*/269270static device_method_t ubt_rtl_methods[] =271{272DEVMETHOD(device_probe, ubt_rtl_probe),273DEVMETHOD_END274};275276DEFINE_CLASS_1(ubt, ubt_rtl_driver, ubt_rtl_methods,277sizeof(struct ubt_softc), ubt_driver);278DRIVER_MODULE(ng_ubt_rtl, uhub, ubt_rtl_driver, 0, 0);279MODULE_VERSION(ng_ubt_rtl, NG_BLUETOOTH_VERSION);280MODULE_DEPEND(ng_ubt_rtl, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);281MODULE_DEPEND(ng_ubt_rtl, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);282MODULE_DEPEND(ng_ubt_rtl, usb, 1, 1, 1);283284285