Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_rtl.c
35065 views
1
/*
2
* ng_ubt_rtl.c
3
*/
4
5
/*-
6
* SPDX-License-Identifier: BSD-2-Clause
7
*
8
* Copyright (c) 2019 Vladimir Kondratyev <[email protected]>
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 8851BE Bluetooth devices */
88
{ USB_VPI(0x13d3, 0x3600, 0) },
89
90
/* Realtek 8852AE Bluetooth devices */
91
{ USB_VPI(0x0bda, 0x2852, 0) },
92
{ USB_VPI(0x0bda, 0xc852, 0) },
93
{ USB_VPI(0x0bda, 0x385a, 0) },
94
{ USB_VPI(0x0bda, 0x4852, 0) },
95
{ USB_VPI(0x04c5, 0x165c, 0) },
96
{ USB_VPI(0x04ca, 0x4006, 0) },
97
{ USB_VPI(0x0cb8, 0xc549, 0) },
98
99
/* Realtek 8852CE Bluetooth devices */
100
{ USB_VPI(0x04ca, 0x4007, 0) },
101
{ USB_VPI(0x04c5, 0x1675, 0) },
102
{ USB_VPI(0x0cb8, 0xc558, 0) },
103
{ USB_VPI(0x13d3, 0x3587, 0) },
104
{ USB_VPI(0x13d3, 0x3586, 0) },
105
{ USB_VPI(0x13d3, 0x3592, 0) },
106
{ USB_VPI(0x0489, 0xe122, 0) },
107
108
/* Realtek 8852BE Bluetooth devices */
109
{ USB_VPI(0x0cb8, 0xc559, 0) },
110
{ USB_VPI(0x0bda, 0x4853, 0) },
111
{ USB_VPI(0x0bda, 0x887b, 0) },
112
{ USB_VPI(0x0bda, 0xb85b, 0) },
113
{ USB_VPI(0x13d3, 0x3570, 0) },
114
{ USB_VPI(0x13d3, 0x3571, 0) },
115
{ USB_VPI(0x13d3, 0x3572, 0) },
116
{ USB_VPI(0x13d3, 0x3591, 0) },
117
{ USB_VPI(0x0489, 0xe123, 0) },
118
{ USB_VPI(0x0489, 0xe125, 0) },
119
120
/* Realtek 8852BT/8852BE-VT Bluetooth devices */
121
{ USB_VPI(0x0bda, 0x8520, 0) },
122
123
/* Realtek 8922AE Bluetooth devices */
124
{ USB_VPI(0x0bda, 0x8922, 0) },
125
{ USB_VPI(0x13d3, 0x3617, 0) },
126
{ USB_VPI(0x13d3, 0x3616, 0) },
127
{ USB_VPI(0x0489, 0xe130, 0) },
128
129
/* Realtek 8723AE Bluetooth devices */
130
{ USB_VPI(0x0930, 0x021d, 0) },
131
{ USB_VPI(0x13d3, 0x3394, 0) },
132
133
/* Realtek 8723BE Bluetooth devices */
134
{ USB_VPI(0x0489, 0xe085, 0) },
135
{ USB_VPI(0x0489, 0xe08b, 0) },
136
{ USB_VPI(0x04f2, 0xb49f, 0) },
137
{ USB_VPI(0x13d3, 0x3410, 0) },
138
{ USB_VPI(0x13d3, 0x3416, 0) },
139
{ USB_VPI(0x13d3, 0x3459, 0) },
140
{ USB_VPI(0x13d3, 0x3494, 0) },
141
142
/* Realtek 8723BU Bluetooth devices */
143
{ USB_VPI(0x7392, 0xa611, 0) },
144
145
/* Realtek 8723DE Bluetooth devices */
146
{ USB_VPI(0x0bda, 0xb009, 0) },
147
{ USB_VPI(0x2ff8, 0xb011, 0) },
148
149
/* Realtek 8761BUV Bluetooth devices */
150
{ USB_VPI(0x2c4e, 0x0115, 0) },
151
{ USB_VPI(0x2357, 0x0604, 0) },
152
{ USB_VPI(0x0b05, 0x190e, 0) },
153
{ USB_VPI(0x2550, 0x8761, 0) },
154
{ USB_VPI(0x0bda, 0x8771, 0) },
155
{ USB_VPI(0x6655, 0x8771, 0) },
156
{ USB_VPI(0x7392, 0xc611, 0) },
157
{ USB_VPI(0x2b89, 0x8761, 0) },
158
159
/* Realtek 8821AE Bluetooth devices */
160
{ USB_VPI(0x0b05, 0x17dc, 0) },
161
{ USB_VPI(0x13d3, 0x3414, 0) },
162
{ USB_VPI(0x13d3, 0x3458, 0) },
163
{ USB_VPI(0x13d3, 0x3461, 0) },
164
{ USB_VPI(0x13d3, 0x3462, 0) },
165
166
/* Realtek 8822BE Bluetooth devices */
167
{ USB_VPI(0x13d3, 0x3526, 0) },
168
{ USB_VPI(0x0b05, 0x185c, 0) },
169
170
/* Realtek 8822CE Bluetooth devices */
171
{ USB_VPI(0x04ca, 0x4005, 0) },
172
{ USB_VPI(0x04c5, 0x161f, 0) },
173
{ USB_VPI(0x0b05, 0x18ef, 0) },
174
{ USB_VPI(0x13d3, 0x3548, 0) },
175
{ USB_VPI(0x13d3, 0x3549, 0) },
176
{ USB_VPI(0x13d3, 0x3553, 0) },
177
{ USB_VPI(0x13d3, 0x3555, 0) },
178
{ USB_VPI(0x2ff8, 0x3051, 0) },
179
{ USB_VPI(0x1358, 0xc123, 0) },
180
{ USB_VPI(0x0bda, 0xc123, 0) },
181
{ USB_VPI(0x0cb5, 0xc547, 0) },
182
};
183
const size_t ubt_rtl_devs_sizeof = sizeof(ubt_rtl_devs);
184
185
/*
186
* List of lmp_subversion values that correspond to Realtek firmwares
187
* incompatible with ng_ubt driver. Alternative firmware for these devices
188
* has to be loaded with rtlbtfw utility. That will trigger lmp_subversion
189
* change to different value.
190
*/
191
static const uint16_t ubt_rtl_lmp_subvers[] = {
192
0x8703, 0x1200, 0x8723, 0x8821,
193
0x8761, 0x8822, 0x8852, 0x8851,
194
};
195
196
/*
197
* Execute generic HCI command and return response in provided buffer.
198
*/
199
200
static usb_error_t
201
ubt_rtl_do_hci_request(struct usb_device *udev, uint16_t opcode,
202
void *resp, uint8_t resp_len)
203
{
204
#define UBT_RTL_HCICMD_TIMEOUT 2000 /* ms */
205
struct ubt_hci_event_command_compl *evt;
206
struct ubt_hci_cmd cmd;
207
usb_error_t error;
208
209
memset(&cmd, 0, sizeof(cmd));
210
cmd.opcode = htole16(opcode);
211
evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) +
212
resp_len, M_TEMP, M_ZERO | M_WAITOK);
213
evt->header.event = NG_HCI_EVENT_COMMAND_COMPL;
214
evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE;
215
216
error = ubt_do_hci_request(udev, &cmd, evt, UBT_RTL_HCICMD_TIMEOUT);
217
if (error != USB_ERR_NORMAL_COMPLETION)
218
goto exit;
219
220
if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL &&
221
evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE)
222
memcpy(resp, evt->data, resp_len);
223
else
224
error = USB_ERR_INVAL;
225
exit:
226
free(evt, M_TEMP);
227
return (error);
228
}
229
230
/*
231
* Probe for a Realtek 87XX/88XX USB Bluetooth device.
232
*/
233
234
static int
235
ubt_rtl_probe(device_t dev)
236
{
237
struct usb_attach_arg *uaa = device_get_ivars(dev);
238
ng_hci_read_local_ver_rp ver;
239
unsigned int i;
240
int error;
241
242
if (uaa->usb_mode != USB_MODE_HOST)
243
return (ENXIO);
244
245
if (uaa->info.bIfaceIndex != 0)
246
return (ENXIO);
247
248
error = usbd_lookup_id_by_uaa(ubt_rtl_devs, sizeof(ubt_rtl_devs), uaa);
249
if (error != 0)
250
return (error);
251
252
if (ubt_rtl_do_hci_request(uaa->device,
253
NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER),
254
&ver, sizeof(ver)) != USB_ERR_NORMAL_COMPLETION)
255
return (ENXIO);
256
257
DPRINTFN(2, "hci_version 0x%02x\n", ver.hci_version);
258
DPRINTFN(2, "hci_revision 0x%04x\n", le16toh(ver.hci_revision));
259
DPRINTFN(2, "lmp_version 0x%02x\n", ver.lmp_version);
260
DPRINTFN(2, "lmp_subversion 0x%04x\n", le16toh(ver.lmp_subversion));
261
262
for (i = 0; i < nitems(ubt_rtl_lmp_subvers); i++)
263
if (le16toh(ver.lmp_subversion) == ubt_rtl_lmp_subvers[i])
264
return (ENXIO);
265
266
return (BUS_PROBE_DEFAULT);
267
}
268
269
/*
270
* Module interface. Attach and detach methods, netgraph node type
271
* registration and PNP string are inherited from ng_ubt.c driver.
272
*/
273
274
static device_method_t ubt_rtl_methods[] =
275
{
276
DEVMETHOD(device_probe, ubt_rtl_probe),
277
DEVMETHOD_END
278
};
279
280
DEFINE_CLASS_1(ubt, ubt_rtl_driver, ubt_rtl_methods,
281
sizeof(struct ubt_softc), ubt_driver);
282
DRIVER_MODULE(ng_ubt_rtl, uhub, ubt_rtl_driver, 0, 0);
283
MODULE_VERSION(ng_ubt_rtl, NG_BLUETOOTH_VERSION);
284
MODULE_DEPEND(ng_ubt_rtl, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
285
MODULE_DEPEND(ng_ubt_rtl, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
286
MODULE_DEPEND(ng_ubt_rtl, usb, 1, 1, 1);
287
288