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