1 /* 2 * ng_ubt_rtl.c 3 */ 4 5 /*- 6 * SPDX-License-Identifier: BSD-2-Clause 7 * 8 * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org> 9 * Copyright (c) 2023 Future Crew LLC. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Attempt to initialize FreeBSD bluetooth stack while Realtek 87XX/88XX USB 35 * device is in bootloader mode locks the adapter hardly so it requires power 36 * on/off cycle to restore. This driver blocks ng_ubt attachment until 37 * operational firmware is loaded by rtlbtfw utility thus avoiding the lock up. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/bus.h> 42 #include <sys/kernel.h> 43 #include <sys/mbuf.h> 44 #include <sys/module.h> 45 #include <sys/systm.h> 46 #include <sys/taskqueue.h> 47 48 #include "usbdevs.h" 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #define USB_DEBUG_VAR usb_debug 52 #include <dev/usb/usb_debug.h> 53 54 #include <netgraph/ng_message.h> 55 #include <netgraph/netgraph.h> 56 #include <netgraph/ng_parse.h> 57 #include <netgraph/bluetooth/include/ng_bluetooth.h> 58 #include <netgraph/bluetooth/include/ng_hci.h> 59 #include <netgraph/bluetooth/include/ng_ubt.h> 60 #include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h> 61 62 static device_probe_t ubt_rtl_probe; 63 64 /* 65 * List of supported bluetooth devices. If you add a new device PID here ensure 66 * that it is blacklisted in ng_ubt.c and is supported by rtlbtfw utility. 67 */ 68 69 const STRUCT_USB_HOST_ID ubt_rtl_devs[] = 70 { 71 /* Generic Realtek Bluetooth class devices */ 72 { USB_VENDOR(USB_VENDOR_REALTEK), 73 USB_IFACE_CLASS(UDCLASS_WIRELESS), 74 USB_IFACE_SUBCLASS(UDSUBCLASS_RF), 75 USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, 76 77 /* Realtek 8821CE Bluetooth devices */ 78 { USB_VPI(0x13d3, 0x3529, 0) }, 79 80 /* Realtek 8822CE Bluetooth devices */ 81 { USB_VPI(0x0bda, 0xb00c, 0) }, 82 { USB_VPI(0x0bda, 0xc822, 0) }, 83 84 /* Realtek 8822CU Bluetooth devices */ 85 { USB_VPI(0x13d3, 0x3549, 0) }, 86 87 /* Realtek 8852AE Bluetooth devices */ 88 { USB_VPI(0x0bda, 0x2852, 0) }, 89 { USB_VPI(0x0bda, 0xc852, 0) }, 90 { USB_VPI(0x0bda, 0x385a, 0) }, 91 { USB_VPI(0x0bda, 0x4852, 0) }, 92 { USB_VPI(0x04c5, 0x165c, 0) }, 93 { USB_VPI(0x04ca, 0x4006, 0) }, 94 { USB_VPI(0x0cb8, 0xc549, 0) }, 95 96 /* Realtek 8852CE Bluetooth devices */ 97 { USB_VPI(0x04ca, 0x4007, 0) }, 98 { USB_VPI(0x04c5, 0x1675, 0) }, 99 { USB_VPI(0x0cb8, 0xc558, 0) }, 100 { USB_VPI(0x13d3, 0x3587, 0) }, 101 { USB_VPI(0x13d3, 0x3586, 0) }, 102 { USB_VPI(0x13d3, 0x3592, 0) }, 103 104 /* Realtek 8852BE Bluetooth devices */ 105 { USB_VPI(0x0cb8, 0xc559, 0) }, 106 { USB_VPI(0x0bda, 0x887b, 0) }, 107 { USB_VPI(0x13d3, 0x3571, 0) }, 108 109 /* Realtek 8723AE Bluetooth devices */ 110 { USB_VPI(0x0930, 0x021d, 0) }, 111 { USB_VPI(0x13d3, 0x3394, 0) }, 112 113 /* Realtek 8723BE Bluetooth devices */ 114 { USB_VPI(0x0489, 0xe085, 0) }, 115 { USB_VPI(0x0489, 0xe08b, 0) }, 116 { USB_VPI(0x04f2, 0xb49f, 0) }, 117 { USB_VPI(0x13d3, 0x3410, 0) }, 118 { USB_VPI(0x13d3, 0x3416, 0) }, 119 { USB_VPI(0x13d3, 0x3459, 0) }, 120 { USB_VPI(0x13d3, 0x3494, 0) }, 121 122 /* Realtek 8723BU Bluetooth devices */ 123 { USB_VPI(0x7392, 0xa611, 0) }, 124 125 /* Realtek 8723DE Bluetooth devices */ 126 { USB_VPI(0x0bda, 0xb009, 0) }, 127 { USB_VPI(0x2ff8, 0xb011, 0) }, 128 129 /* Realtek 8761BUV Bluetooth devices */ 130 { USB_VPI(0x2357, 0x0604, 0) }, 131 { USB_VPI(0x0b05, 0x190e, 0) }, 132 { USB_VPI(0x2550, 0x8761, 0) }, 133 { USB_VPI(0x0bda, 0x8771, 0) }, 134 { USB_VPI(0x6655, 0x8771, 0) }, 135 { USB_VPI(0x7392, 0xc611, 0) }, 136 { USB_VPI(0x2b89, 0x8761, 0) }, 137 138 /* Realtek 8821AE Bluetooth devices */ 139 { USB_VPI(0x0b05, 0x17dc, 0) }, 140 { USB_VPI(0x13d3, 0x3414, 0) }, 141 { USB_VPI(0x13d3, 0x3458, 0) }, 142 { USB_VPI(0x13d3, 0x3461, 0) }, 143 { USB_VPI(0x13d3, 0x3462, 0) }, 144 145 /* Realtek 8822BE Bluetooth devices */ 146 { USB_VPI(0x13d3, 0x3526, 0) }, 147 { USB_VPI(0x0b05, 0x185c, 0) }, 148 149 /* Realtek 8822CE Bluetooth devices */ 150 { USB_VPI(0x04ca, 0x4005, 0) }, 151 { USB_VPI(0x04c5, 0x161f, 0) }, 152 { USB_VPI(0x0b05, 0x18ef, 0) }, 153 { USB_VPI(0x13d3, 0x3548, 0) }, 154 { USB_VPI(0x13d3, 0x3549, 0) }, 155 { USB_VPI(0x13d3, 0x3553, 0) }, 156 { USB_VPI(0x13d3, 0x3555, 0) }, 157 { USB_VPI(0x2ff8, 0x3051, 0) }, 158 { USB_VPI(0x1358, 0xc123, 0) }, 159 { USB_VPI(0x0bda, 0xc123, 0) }, 160 { USB_VPI(0x0cb5, 0xc547, 0) }, 161 }; 162 const size_t ubt_rtl_devs_sizeof = sizeof(ubt_rtl_devs); 163 164 /* 165 * List of lmp_subversion values that correspond to Realtek firmwares 166 * incompatible with ng_ubt driver. Alternative firmware for these devices 167 * has to be loaded with rtlbtfw utility. That will trigger lmp_subversion 168 * change to different value. 169 */ 170 static const uint16_t ubt_rtl_lmp_subvers[] = { 171 0x8703, 0x1200, 0x8723, 0x8821, 172 0x8761, 0x8822, 0x8852, 0x8851, 173 }; 174 175 /* 176 * Execute generic HCI command and return response in provided buffer. 177 */ 178 179 static usb_error_t 180 ubt_rtl_do_hci_request(struct usb_device *udev, uint16_t opcode, 181 void *resp, uint8_t resp_len) 182 { 183 #define UBT_RTL_HCICMD_TIMEOUT 2000 /* ms */ 184 struct ubt_hci_event_command_compl *evt; 185 struct ubt_hci_cmd cmd; 186 usb_error_t error; 187 188 memset(&cmd, 0, sizeof(cmd)); 189 cmd.opcode = htole16(opcode); 190 evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) + 191 resp_len, M_TEMP, M_ZERO | M_WAITOK); 192 evt->header.event = NG_HCI_EVENT_COMMAND_COMPL; 193 evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE; 194 195 error = ubt_do_hci_request(udev, &cmd, evt, UBT_RTL_HCICMD_TIMEOUT); 196 if (error != USB_ERR_NORMAL_COMPLETION) 197 goto exit; 198 199 if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL && 200 evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE) 201 memcpy(resp, evt->data, resp_len); 202 else 203 error = USB_ERR_INVAL; 204 exit: 205 free(evt, M_TEMP); 206 return (error); 207 } 208 209 /* 210 * Probe for a Realtek 87XX/88XX USB Bluetooth device. 211 */ 212 213 static int 214 ubt_rtl_probe(device_t dev) 215 { 216 struct usb_attach_arg *uaa = device_get_ivars(dev); 217 ng_hci_read_local_ver_rp ver; 218 unsigned int i; 219 int error; 220 221 if (uaa->usb_mode != USB_MODE_HOST) 222 return (ENXIO); 223 224 if (uaa->info.bIfaceIndex != 0) 225 return (ENXIO); 226 227 error = usbd_lookup_id_by_uaa(ubt_rtl_devs, sizeof(ubt_rtl_devs), uaa); 228 if (error != 0) 229 return (error); 230 231 if (ubt_rtl_do_hci_request(uaa->device, 232 NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER), 233 &ver, sizeof(ver)) != USB_ERR_NORMAL_COMPLETION) 234 return (ENXIO); 235 236 DPRINTFN(2, "hci_version 0x%02x\n", ver.hci_version); 237 DPRINTFN(2, "hci_revision 0x%04x\n", le16toh(ver.hci_revision)); 238 DPRINTFN(2, "lmp_version 0x%02x\n", ver.lmp_version); 239 DPRINTFN(2, "lmp_subversion 0x%04x\n", le16toh(ver.lmp_subversion)); 240 241 for (i = 0; i < nitems(ubt_rtl_lmp_subvers); i++) 242 if (le16toh(ver.lmp_subversion) == ubt_rtl_lmp_subvers[i]) 243 return (ENXIO); 244 245 return (BUS_PROBE_DEFAULT); 246 } 247 248 /* 249 * Module interface. Attach and detach methods, netgraph node type 250 * registration and PNP string are inherited from ng_ubt.c driver. 251 */ 252 253 static device_method_t ubt_rtl_methods[] = 254 { 255 DEVMETHOD(device_probe, ubt_rtl_probe), 256 DEVMETHOD_END 257 }; 258 259 DEFINE_CLASS_1(ubt, ubt_rtl_driver, ubt_rtl_methods, 260 sizeof(struct ubt_softc), ubt_driver); 261 DRIVER_MODULE(ng_ubt_rtl, uhub, ubt_rtl_driver, 0, 0); 262 MODULE_VERSION(ng_ubt_rtl, NG_BLUETOOTH_VERSION); 263 MODULE_DEPEND(ng_ubt_rtl, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 264 MODULE_DEPEND(ng_ubt_rtl, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION); 265 MODULE_DEPEND(ng_ubt_rtl, usb, 1, 1, 1); 266