]>
Commit | Line | Data |
---|---|---|
5e23b923 MH |
1 | /* |
2 | * | |
3 | * Generic Bluetooth USB driver | |
4 | * | |
9bfa35fe | 5 | * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org> |
5e23b923 MH |
6 | * |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
6a50b943 | 24 | #include <linux/dmi.h> |
5e23b923 | 25 | #include <linux/module.h> |
5e23b923 | 26 | #include <linux/usb.h> |
18970430 | 27 | #include <linux/usb/quirks.h> |
dffd30ee | 28 | #include <linux/firmware.h> |
fd913ef7 RJ |
29 | #include <linux/of_device.h> |
30 | #include <linux/of_irq.h> | |
84845ceb | 31 | #include <linux/pci.h> |
017789f3 | 32 | #include <linux/suspend.h> |
a2698a9b | 33 | #include <asm/unaligned.h> |
5e23b923 MH |
34 | |
35 | #include <net/bluetooth/bluetooth.h> | |
36 | #include <net/bluetooth/hci_core.h> | |
37 | ||
4185a0f5 | 38 | #include "btintel.h" |
1df1f591 | 39 | #include "btbcm.h" |
db33c77d | 40 | #include "btrtl.h" |
1df1f591 | 41 | |
34dced9b | 42 | #define VERSION "0.8" |
cfeb4145 | 43 | |
90ab5ee9 RR |
44 | static bool disable_scofix; |
45 | static bool force_scofix; | |
7a9d4020 | 46 | |
917a3337 | 47 | static bool reset = true; |
cfeb4145 MH |
48 | |
49 | static struct usb_driver btusb_driver; | |
50 | ||
51 | #define BTUSB_IGNORE 0x01 | |
7a9d4020 MH |
52 | #define BTUSB_DIGIANSWER 0x02 |
53 | #define BTUSB_CSR 0x04 | |
54 | #define BTUSB_SNIFFER 0x08 | |
55 | #define BTUSB_BCM92035 0x10 | |
56 | #define BTUSB_BROKEN_ISOC 0x20 | |
57 | #define BTUSB_WRONG_SCO_MTU 0x40 | |
2d25f8b4 | 58 | #define BTUSB_ATH3012 0x80 |
dffd30ee | 59 | #define BTUSB_INTEL 0x100 |
40df783d MH |
60 | #define BTUSB_INTEL_BOOT 0x200 |
61 | #define BTUSB_BCM_PATCHRAM 0x400 | |
ae8df494 | 62 | #define BTUSB_MARVELL 0x800 |
4fcef8ed | 63 | #define BTUSB_SWAVE 0x1000 |
cda0dd78 | 64 | #define BTUSB_INTEL_NEW 0x2000 |
893ba544 | 65 | #define BTUSB_AMP 0x4000 |
3267c884 | 66 | #define BTUSB_QCA_ROME 0x8000 |
17b2772b | 67 | #define BTUSB_BCM_APPLE 0x10000 |
a2698a9b | 68 | #define BTUSB_REALTEK 0x20000 |
6c9d435d | 69 | #define BTUSB_BCM2045 0x40000 |
22f8e9db | 70 | #define BTUSB_IFNUM_2 0x80000 |
418678b0 | 71 | #define BTUSB_CW6622 0x100000 |
5e23b923 | 72 | |
54265202 | 73 | static const struct usb_device_id btusb_table[] = { |
5e23b923 MH |
74 | /* Generic Bluetooth USB device */ |
75 | { USB_DEVICE_INFO(0xe0, 0x01, 0x01) }, | |
76 | ||
893ba544 MH |
77 | /* Generic Bluetooth AMP device */ |
78 | { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP }, | |
79 | ||
d63b2826 DD |
80 | /* Generic Bluetooth USB interface */ |
81 | { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) }, | |
82 | ||
1fa6535f | 83 | /* Apple-specific (Broadcom) devices */ |
17b2772b | 84 | { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01), |
22f8e9db | 85 | .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 }, |
1fa6535f | 86 | |
178c059e CYC |
87 | /* MediaTek MT76x0E */ |
88 | { USB_DEVICE(0x0e8d, 0x763f) }, | |
89 | ||
c510eae3 | 90 | /* Broadcom SoftSailing reporting vendor specific */ |
2e8b5063 | 91 | { USB_DEVICE(0x0a5c, 0x21e1) }, |
c510eae3 | 92 | |
3cd01976 NI |
93 | /* Apple MacBookPro 7,1 */ |
94 | { USB_DEVICE(0x05ac, 0x8213) }, | |
95 | ||
0a79f674 CL |
96 | /* Apple iMac11,1 */ |
97 | { USB_DEVICE(0x05ac, 0x8215) }, | |
98 | ||
9c047157 NI |
99 | /* Apple MacBookPro6,2 */ |
100 | { USB_DEVICE(0x05ac, 0x8218) }, | |
101 | ||
3e3ede7d EH |
102 | /* Apple MacBookAir3,1, MacBookAir3,2 */ |
103 | { USB_DEVICE(0x05ac, 0x821b) }, | |
104 | ||
a63b723d PAVM |
105 | /* Apple MacBookAir4,1 */ |
106 | { USB_DEVICE(0x05ac, 0x821f) }, | |
107 | ||
88d377b6 MAP |
108 | /* Apple MacBookPro8,2 */ |
109 | { USB_DEVICE(0x05ac, 0x821a) }, | |
110 | ||
f78b6826 JK |
111 | /* Apple MacMini5,1 */ |
112 | { USB_DEVICE(0x05ac, 0x8281) }, | |
113 | ||
cfeb4145 | 114 | /* AVM BlueFRITZ! USB v2.0 */ |
4fcef8ed | 115 | { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE }, |
cfeb4145 MH |
116 | |
117 | /* Bluetooth Ultraport Module from IBM */ | |
118 | { USB_DEVICE(0x04bf, 0x030a) }, | |
119 | ||
120 | /* ALPS Modules with non-standard id */ | |
121 | { USB_DEVICE(0x044e, 0x3001) }, | |
122 | { USB_DEVICE(0x044e, 0x3002) }, | |
123 | ||
124 | /* Ericsson with non-standard id */ | |
125 | { USB_DEVICE(0x0bdb, 0x1002) }, | |
126 | ||
127 | /* Canyon CN-BTU1 with HID interfaces */ | |
7a9d4020 | 128 | { USB_DEVICE(0x0c10, 0x0000) }, |
cfeb4145 | 129 | |
d13431ca WJS |
130 | /* Broadcom BCM20702A0 */ |
131 | { USB_DEVICE(0x413c, 0x8197) }, | |
132 | ||
d049f4e5 MH |
133 | /* Broadcom BCM20702B0 (Dynex/Insignia) */ |
134 | { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM }, | |
135 | ||
2faf71ce | 136 | /* Broadcom BCM43142A0 (Foxconn/Lenovo) */ |
628c26b4 DT |
137 | { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01), |
138 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
2faf71ce | 139 | |
a57bac43 CH |
140 | /* Broadcom BCM920703 (HTC Vive) */ |
141 | { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01), | |
142 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
143 | ||
98514036 | 144 | /* Foxconn - Hon Hai */ |
6029ddc2 HS |
145 | { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01), |
146 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
98514036 | 147 | |
8f0c304c MD |
148 | /* Lite-On Technology - Broadcom based */ |
149 | { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01), | |
150 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
151 | ||
0b880062 | 152 | /* Broadcom devices with vendor specific id */ |
10d4c673 PG |
153 | { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01), |
154 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
92c385f4 | 155 | |
c2aef6e8 | 156 | /* ASUSTek Computer - Broadcom based */ |
9a5abdaa RD |
157 | { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01), |
158 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
c2aef6e8 | 159 | |
5bcecf32 | 160 | /* Belkin F8065bf - Broadcom based */ |
6331c686 MH |
161 | { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01), |
162 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
5bcecf32 | 163 | |
9113bfd8 | 164 | /* IMC Networks - Broadcom based */ |
6331c686 MH |
165 | { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01), |
166 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
9113bfd8 | 167 | |
fdfddc60 WJS |
168 | /* Dell Computer - Broadcom based */ |
169 | { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01), | |
170 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
171 | ||
1623d0bf DT |
172 | /* Toshiba Corp - Broadcom based */ |
173 | { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01), | |
174 | .driver_info = BTUSB_BCM_PATCHRAM }, | |
175 | ||
40df783d | 176 | /* Intel Bluetooth USB Bootloader (RAM module) */ |
d92f2df0 MH |
177 | { USB_DEVICE(0x8087, 0x0a5a), |
178 | .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC }, | |
40df783d | 179 | |
5e23b923 MH |
180 | { } /* Terminating entry */ |
181 | }; | |
182 | ||
183 | MODULE_DEVICE_TABLE(usb, btusb_table); | |
184 | ||
54265202 | 185 | static const struct usb_device_id blacklist_table[] = { |
cfeb4145 MH |
186 | /* CSR BlueCore devices */ |
187 | { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR }, | |
188 | ||
189 | /* Broadcom BCM2033 without firmware */ | |
190 | { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE }, | |
191 | ||
6c9d435d MH |
192 | /* Broadcom BCM2045 devices */ |
193 | { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 }, | |
194 | ||
be93112a | 195 | /* Atheros 3011 with sflash firmware */ |
0b880062 AS |
196 | { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE }, |
197 | { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE }, | |
2eeff0b4 | 198 | { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE }, |
0b880062 | 199 | { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE }, |
be93112a | 200 | { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE }, |
6eda541d | 201 | { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE }, |
2a7bcccc | 202 | { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE }, |
be93112a | 203 | |
509e7861 CYC |
204 | /* Atheros AR9285 Malbec with sflash firmware */ |
205 | { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE }, | |
206 | ||
d9f51b51 | 207 | /* Atheros 3012 with sflash firmware */ |
0b880062 AS |
208 | { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 }, |
209 | { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 }, | |
210 | { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 }, | |
211 | { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 }, | |
212 | { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 }, | |
692c062e | 213 | { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 }, |
4b552bc9 | 214 | { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 }, |
28c971d8 | 215 | { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 }, |
0b880062 AS |
216 | { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 }, |
217 | { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 }, | |
218 | { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 }, | |
219 | { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 }, | |
1fb4e09a | 220 | { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 }, |
0b880062 AS |
221 | { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 }, |
222 | { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 }, | |
7e730c7f | 223 | { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 }, |
ec0810d2 | 224 | { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 }, |
134d3b35 | 225 | { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 }, |
81d90442 | 226 | { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 }, |
441ad62d | 227 | { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 }, |
0b880062 | 228 | { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 }, |
cd355ff0 | 229 | { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 }, |
0b880062 | 230 | { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 }, |
89d2975f | 231 | { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 }, |
a735f9e2 | 232 | { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 }, |
d66629c1 | 233 | { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 }, |
b5f01a51 | 234 | { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 }, |
94a32d10 | 235 | { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 }, |
07c0ea87 | 236 | { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 }, |
b131237c | 237 | { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 }, |
1e56f1eb | 238 | { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 }, |
0b880062 | 239 | { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 }, |
ebaf5795 | 240 | { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 }, |
18e0afab | 241 | { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 }, |
0b880062 | 242 | { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 }, |
ac71311e | 243 | { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 }, |
0a3658cc | 244 | { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 }, |
ca79f232 | 245 | { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 }, |
0b880062 AS |
246 | { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 }, |
247 | { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 }, | |
eed307e2 | 248 | { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 }, |
609574eb | 249 | { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 }, |
5b77a1f3 | 250 | { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 }, |
3bb30a7c | 251 | { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 }, |
033efa92 | 252 | { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 }, |
fa2f1394 | 253 | { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 }, |
75c6aca4 | 254 | { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 }, |
0d0cef61 | 255 | { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 }, |
72f9f8b5 | 256 | { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 }, |
12d86896 | 257 | { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 }, |
d9f51b51 | 258 | |
e9036e33 CYC |
259 | /* Atheros AR5BBU12 with sflash firmware */ |
260 | { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE }, | |
261 | ||
85d59726 | 262 | /* Atheros AR5BBU12 with sflash firmware */ |
bc21fde2 | 263 | { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 }, |
0b880062 | 264 | { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 }, |
85d59726 | 265 | |
3267c884 | 266 | /* QCA ROME chipset */ |
4cec7785 | 267 | { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME }, |
2054111b | 268 | { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME }, |
93519934 | 269 | { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME }, |
bc1d04cf | 270 | { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME }, |
c9e44474 | 271 | { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME }, |
1eef1c35 | 272 | { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME }, |
c9e44474 | 273 | { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME }, |
47ca5898 | 274 | { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME }, |
858ff38a | 275 | { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME }, |
06e41d8a | 276 | { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME }, |
1144a4ee | 277 | { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME }, |
022ffe77 | 278 | { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME }, |
d829b9e2 | 279 | { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME }, |
aae30264 | 280 | { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME }, |
3267c884 | 281 | |
cfeb4145 | 282 | /* Broadcom BCM2035 */ |
7a9d4020 | 283 | { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 }, |
0b880062 AS |
284 | { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU }, |
285 | { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
cfeb4145 MH |
286 | |
287 | /* Broadcom BCM2045 */ | |
7a9d4020 MH |
288 | { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU }, |
289 | { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
bdbef3d6 | 290 | |
cfeb4145 | 291 | /* IBM/Lenovo ThinkPad with Broadcom chip */ |
7a9d4020 MH |
292 | { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU }, |
293 | { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
cfeb4145 MH |
294 | |
295 | /* HP laptop with Broadcom chip */ | |
7a9d4020 | 296 | { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU }, |
cfeb4145 MH |
297 | |
298 | /* Dell laptop with Broadcom chip */ | |
7a9d4020 | 299 | { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU }, |
cfeb4145 | 300 | |
5ddd4a60 | 301 | /* Dell Wireless 370 and 410 devices */ |
7a9d4020 | 302 | { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU }, |
5ddd4a60 | 303 | { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU }, |
cfeb4145 | 304 | |
7a9d4020 MH |
305 | /* Belkin F8T012 and F8T013 devices */ |
306 | { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
307 | { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
cfeb4145 | 308 | |
5ddd4a60 MH |
309 | /* Asus WL-BTD202 device */ |
310 | { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
311 | ||
312 | /* Kensington Bluetooth USB adapter */ | |
313 | { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU }, | |
314 | ||
cfeb4145 MH |
315 | /* RTX Telecom based adapters with buggy SCO support */ |
316 | { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC }, | |
317 | { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC }, | |
318 | ||
319 | /* CONWISE Technology based adapters with buggy SCO support */ | |
418678b0 SJ |
320 | { USB_DEVICE(0x0e5e, 0x6622), |
321 | .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622}, | |
cfeb4145 | 322 | |
4fcef8ed | 323 | /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */ |
2eeac871 | 324 | { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE }, |
4fcef8ed | 325 | |
cfeb4145 MH |
326 | /* Digianswer devices */ |
327 | { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER }, | |
328 | { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE }, | |
329 | ||
330 | /* CSR BlueCore Bluetooth Sniffer */ | |
4f64fa80 MH |
331 | { USB_DEVICE(0x0a12, 0x0002), |
332 | .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC }, | |
cfeb4145 MH |
333 | |
334 | /* Frontline ComProbe Bluetooth Sniffer */ | |
4f64fa80 MH |
335 | { USB_DEVICE(0x16d3, 0x0002), |
336 | .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC }, | |
cfeb4145 | 337 | |
cb1ee89f MH |
338 | /* Marvell Bluetooth devices */ |
339 | { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL }, | |
340 | { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL }, | |
1165df0e | 341 | { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL }, |
cb1ee89f | 342 | |
d0ac9eb7 | 343 | /* Intel Bluetooth devices */ |
de766142 | 344 | { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW }, |
d6608ee5 | 345 | { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW }, |
424443b7 | 346 | { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW }, |
407550fe | 347 | { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR }, |
dffd30ee | 348 | { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL }, |
ef4e5e4a | 349 | { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL }, |
cda0dd78 | 350 | { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW }, |
439e65d3 | 351 | { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL }, |
86a6129a | 352 | { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW }, |
dffd30ee | 353 | |
d0ac9eb7 MH |
354 | /* Other Intel Bluetooth devices */ |
355 | { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01), | |
356 | .driver_info = BTUSB_IGNORE }, | |
ae8df494 | 357 | |
a2698a9b DD |
358 | /* Realtek Bluetooth devices */ |
359 | { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01), | |
360 | .driver_info = BTUSB_REALTEK }, | |
361 | ||
362 | /* Additional Realtek 8723AE Bluetooth devices */ | |
363 | { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK }, | |
364 | { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK }, | |
365 | ||
366 | /* Additional Realtek 8723BE Bluetooth devices */ | |
367 | { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK }, | |
368 | { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK }, | |
369 | { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK }, | |
370 | { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK }, | |
371 | { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK }, | |
a81d72d2 | 372 | { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK }, |
a2698a9b | 373 | |
56ae08ec VB |
374 | /* Additional Realtek 8723BU Bluetooth devices */ |
375 | { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK }, | |
376 | ||
76ef4f86 JHP |
377 | /* Additional Realtek 8723DE Bluetooth devices */ |
378 | { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK }, | |
379 | ||
a2698a9b DD |
380 | /* Additional Realtek 8821AE Bluetooth devices */ |
381 | { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK }, | |
382 | { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK }, | |
383 | { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK }, | |
384 | { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK }, | |
385 | { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK }, | |
71889af6 LF |
386 | |
387 | /* Additional Realtek 8822BE Bluetooth devices */ | |
388 | { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK }, | |
a2698a9b | 389 | |
4481c076 PP |
390 | /* Silicon Wave based devices */ |
391 | { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE }, | |
392 | ||
5e23b923 MH |
393 | { } /* Terminating entry */ |
394 | }; | |
395 | ||
6a50b943 HG |
396 | /* The Bluetooth USB module build into some devices needs to be reset on resume, |
397 | * this is a problem with the platform (likely shutting off all power) not with | |
398 | * the module itself. So we use a DMI list to match known broken platforms. | |
399 | */ | |
400 | static const struct dmi_system_id btusb_needs_reset_resume_table[] = { | |
ed9e1bb3 KHF |
401 | { |
402 | /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */ | |
403 | .matches = { | |
404 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
405 | DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"), | |
406 | }, | |
407 | }, | |
9bf1741c HG |
408 | { |
409 | /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */ | |
410 | .matches = { | |
411 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
412 | DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"), | |
413 | }, | |
414 | }, | |
6a50b943 HG |
415 | {} |
416 | }; | |
417 | ||
9bfa35fe MH |
418 | #define BTUSB_MAX_ISOC_FRAMES 10 |
419 | ||
5e23b923 MH |
420 | #define BTUSB_INTR_RUNNING 0 |
421 | #define BTUSB_BULK_RUNNING 1 | |
9bfa35fe | 422 | #define BTUSB_ISOC_RUNNING 2 |
7bee549e | 423 | #define BTUSB_SUSPENDING 3 |
08b8b6c4 | 424 | #define BTUSB_DID_ISO_RESUME 4 |
cda0dd78 MH |
425 | #define BTUSB_BOOTLOADER 5 |
426 | #define BTUSB_DOWNLOADING 6 | |
ce6bb929 | 427 | #define BTUSB_FIRMWARE_LOADED 7 |
cda0dd78 | 428 | #define BTUSB_FIRMWARE_FAILED 8 |
ce6bb929 | 429 | #define BTUSB_BOOTING 9 |
18970430 HG |
430 | #define BTUSB_DIAG_RUNNING 10 |
431 | #define BTUSB_OOB_WAKE_ENABLED 11 | |
5e23b923 MH |
432 | |
433 | struct btusb_data { | |
434 | struct hci_dev *hdev; | |
435 | struct usb_device *udev; | |
5fbcd260 | 436 | struct usb_interface *intf; |
9bfa35fe | 437 | struct usb_interface *isoc; |
9d08f504 | 438 | struct usb_interface *diag; |
459232fc | 439 | unsigned isoc_ifnum; |
5e23b923 | 440 | |
5e23b923 MH |
441 | unsigned long flags; |
442 | ||
443 | struct work_struct work; | |
7bee549e | 444 | struct work_struct waker; |
5e23b923 | 445 | |
803b5836 | 446 | struct usb_anchor deferred; |
5e23b923 | 447 | struct usb_anchor tx_anchor; |
803b5836 MH |
448 | int tx_in_flight; |
449 | spinlock_t txlock; | |
450 | ||
5e23b923 MH |
451 | struct usb_anchor intr_anchor; |
452 | struct usb_anchor bulk_anchor; | |
9bfa35fe | 453 | struct usb_anchor isoc_anchor; |
9d08f504 | 454 | struct usb_anchor diag_anchor; |
803b5836 MH |
455 | spinlock_t rxlock; |
456 | ||
457 | struct sk_buff *evt_skb; | |
458 | struct sk_buff *acl_skb; | |
459 | struct sk_buff *sco_skb; | |
5e23b923 MH |
460 | |
461 | struct usb_endpoint_descriptor *intr_ep; | |
462 | struct usb_endpoint_descriptor *bulk_tx_ep; | |
463 | struct usb_endpoint_descriptor *bulk_rx_ep; | |
9bfa35fe MH |
464 | struct usb_endpoint_descriptor *isoc_tx_ep; |
465 | struct usb_endpoint_descriptor *isoc_rx_ep; | |
9d08f504 MH |
466 | struct usb_endpoint_descriptor *diag_tx_ep; |
467 | struct usb_endpoint_descriptor *diag_rx_ep; | |
9bfa35fe | 468 | |
7a9d4020 | 469 | __u8 cmdreq_type; |
893ba544 | 470 | __u8 cmdreq; |
7a9d4020 | 471 | |
43c2e57f | 472 | unsigned int sco_num; |
9bfa35fe | 473 | int isoc_altsetting; |
6a88adf2 | 474 | int suspend_count; |
2cbd3f5c | 475 | |
97307f51 | 476 | int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb); |
2cbd3f5c | 477 | int (*recv_bulk)(struct btusb_data *data, void *buffer, int count); |
ace31982 KBYT |
478 | |
479 | int (*setup_on_usb)(struct hci_dev *hdev); | |
fd913ef7 RJ |
480 | |
481 | int oob_wake_irq; /* irq for out-of-band wake-on-bt */ | |
5e23b923 MH |
482 | }; |
483 | ||
803b5836 MH |
484 | static inline void btusb_free_frags(struct btusb_data *data) |
485 | { | |
486 | unsigned long flags; | |
487 | ||
488 | spin_lock_irqsave(&data->rxlock, flags); | |
489 | ||
490 | kfree_skb(data->evt_skb); | |
491 | data->evt_skb = NULL; | |
492 | ||
493 | kfree_skb(data->acl_skb); | |
494 | data->acl_skb = NULL; | |
495 | ||
496 | kfree_skb(data->sco_skb); | |
497 | data->sco_skb = NULL; | |
498 | ||
499 | spin_unlock_irqrestore(&data->rxlock, flags); | |
500 | } | |
501 | ||
1ffa4ad0 MH |
502 | static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count) |
503 | { | |
803b5836 MH |
504 | struct sk_buff *skb; |
505 | int err = 0; | |
506 | ||
507 | spin_lock(&data->rxlock); | |
508 | skb = data->evt_skb; | |
509 | ||
510 | while (count) { | |
511 | int len; | |
512 | ||
513 | if (!skb) { | |
514 | skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC); | |
515 | if (!skb) { | |
516 | err = -ENOMEM; | |
517 | break; | |
518 | } | |
519 | ||
618e8bc2 MH |
520 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; |
521 | hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE; | |
803b5836 MH |
522 | } |
523 | ||
618e8bc2 | 524 | len = min_t(uint, hci_skb_expect(skb), count); |
59ae1d12 | 525 | skb_put_data(skb, buffer, len); |
803b5836 MH |
526 | |
527 | count -= len; | |
528 | buffer += len; | |
618e8bc2 | 529 | hci_skb_expect(skb) -= len; |
803b5836 MH |
530 | |
531 | if (skb->len == HCI_EVENT_HDR_SIZE) { | |
532 | /* Complete event header */ | |
618e8bc2 | 533 | hci_skb_expect(skb) = hci_event_hdr(skb)->plen; |
803b5836 | 534 | |
618e8bc2 | 535 | if (skb_tailroom(skb) < hci_skb_expect(skb)) { |
803b5836 MH |
536 | kfree_skb(skb); |
537 | skb = NULL; | |
538 | ||
539 | err = -EILSEQ; | |
540 | break; | |
541 | } | |
542 | } | |
543 | ||
618e8bc2 | 544 | if (!hci_skb_expect(skb)) { |
803b5836 | 545 | /* Complete frame */ |
97307f51 | 546 | data->recv_event(data->hdev, skb); |
803b5836 MH |
547 | skb = NULL; |
548 | } | |
549 | } | |
550 | ||
551 | data->evt_skb = skb; | |
552 | spin_unlock(&data->rxlock); | |
553 | ||
554 | return err; | |
1ffa4ad0 MH |
555 | } |
556 | ||
557 | static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count) | |
558 | { | |
803b5836 MH |
559 | struct sk_buff *skb; |
560 | int err = 0; | |
561 | ||
562 | spin_lock(&data->rxlock); | |
563 | skb = data->acl_skb; | |
564 | ||
565 | while (count) { | |
566 | int len; | |
567 | ||
568 | if (!skb) { | |
569 | skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC); | |
570 | if (!skb) { | |
571 | err = -ENOMEM; | |
572 | break; | |
573 | } | |
574 | ||
618e8bc2 MH |
575 | hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT; |
576 | hci_skb_expect(skb) = HCI_ACL_HDR_SIZE; | |
803b5836 MH |
577 | } |
578 | ||
618e8bc2 | 579 | len = min_t(uint, hci_skb_expect(skb), count); |
59ae1d12 | 580 | skb_put_data(skb, buffer, len); |
803b5836 MH |
581 | |
582 | count -= len; | |
583 | buffer += len; | |
618e8bc2 | 584 | hci_skb_expect(skb) -= len; |
803b5836 MH |
585 | |
586 | if (skb->len == HCI_ACL_HDR_SIZE) { | |
587 | __le16 dlen = hci_acl_hdr(skb)->dlen; | |
588 | ||
589 | /* Complete ACL header */ | |
618e8bc2 | 590 | hci_skb_expect(skb) = __le16_to_cpu(dlen); |
803b5836 | 591 | |
618e8bc2 | 592 | if (skb_tailroom(skb) < hci_skb_expect(skb)) { |
803b5836 MH |
593 | kfree_skb(skb); |
594 | skb = NULL; | |
595 | ||
596 | err = -EILSEQ; | |
597 | break; | |
598 | } | |
599 | } | |
600 | ||
618e8bc2 | 601 | if (!hci_skb_expect(skb)) { |
803b5836 MH |
602 | /* Complete frame */ |
603 | hci_recv_frame(data->hdev, skb); | |
604 | skb = NULL; | |
605 | } | |
606 | } | |
607 | ||
608 | data->acl_skb = skb; | |
609 | spin_unlock(&data->rxlock); | |
610 | ||
611 | return err; | |
1ffa4ad0 MH |
612 | } |
613 | ||
614 | static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count) | |
615 | { | |
803b5836 MH |
616 | struct sk_buff *skb; |
617 | int err = 0; | |
618 | ||
619 | spin_lock(&data->rxlock); | |
620 | skb = data->sco_skb; | |
621 | ||
622 | while (count) { | |
623 | int len; | |
624 | ||
625 | if (!skb) { | |
626 | skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC); | |
627 | if (!skb) { | |
628 | err = -ENOMEM; | |
629 | break; | |
630 | } | |
631 | ||
618e8bc2 MH |
632 | hci_skb_pkt_type(skb) = HCI_SCODATA_PKT; |
633 | hci_skb_expect(skb) = HCI_SCO_HDR_SIZE; | |
803b5836 MH |
634 | } |
635 | ||
618e8bc2 | 636 | len = min_t(uint, hci_skb_expect(skb), count); |
59ae1d12 | 637 | skb_put_data(skb, buffer, len); |
803b5836 MH |
638 | |
639 | count -= len; | |
640 | buffer += len; | |
618e8bc2 | 641 | hci_skb_expect(skb) -= len; |
803b5836 MH |
642 | |
643 | if (skb->len == HCI_SCO_HDR_SIZE) { | |
644 | /* Complete SCO header */ | |
618e8bc2 | 645 | hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen; |
803b5836 | 646 | |
618e8bc2 | 647 | if (skb_tailroom(skb) < hci_skb_expect(skb)) { |
803b5836 MH |
648 | kfree_skb(skb); |
649 | skb = NULL; | |
650 | ||
651 | err = -EILSEQ; | |
652 | break; | |
653 | } | |
654 | } | |
655 | ||
618e8bc2 | 656 | if (!hci_skb_expect(skb)) { |
803b5836 MH |
657 | /* Complete frame */ |
658 | hci_recv_frame(data->hdev, skb); | |
659 | skb = NULL; | |
660 | } | |
661 | } | |
662 | ||
663 | data->sco_skb = skb; | |
664 | spin_unlock(&data->rxlock); | |
665 | ||
666 | return err; | |
1ffa4ad0 MH |
667 | } |
668 | ||
5e23b923 MH |
669 | static void btusb_intr_complete(struct urb *urb) |
670 | { | |
671 | struct hci_dev *hdev = urb->context; | |
155961e8 | 672 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
673 | int err; |
674 | ||
89e7533d MH |
675 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, |
676 | urb->actual_length); | |
5e23b923 MH |
677 | |
678 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
679 | return; | |
680 | ||
681 | if (urb->status == 0) { | |
9bfa35fe MH |
682 | hdev->stat.byte_rx += urb->actual_length; |
683 | ||
1ffa4ad0 MH |
684 | if (btusb_recv_intr(data, urb->transfer_buffer, |
685 | urb->actual_length) < 0) { | |
2064ee33 | 686 | bt_dev_err(hdev, "corrupted event packet"); |
5e23b923 MH |
687 | hdev->stat.err_rx++; |
688 | } | |
85560c4a CC |
689 | } else if (urb->status == -ENOENT) { |
690 | /* Avoid suspend failed when usb_kill_urb */ | |
691 | return; | |
5e23b923 MH |
692 | } |
693 | ||
694 | if (!test_bit(BTUSB_INTR_RUNNING, &data->flags)) | |
695 | return; | |
696 | ||
7bee549e | 697 | usb_mark_last_busy(data->udev); |
5e23b923 MH |
698 | usb_anchor_urb(urb, &data->intr_anchor); |
699 | ||
700 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
701 | if (err < 0) { | |
4935f1c1 | 702 | /* -EPERM: urb is being killed; |
d98422cb DR |
703 | * -ENODEV: device got disconnected |
704 | */ | |
4935f1c1 | 705 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
706 | bt_dev_err(hdev, "urb %p failed to resubmit (%d)", |
707 | urb, -err); | |
5e23b923 MH |
708 | usb_unanchor_urb(urb); |
709 | } | |
710 | } | |
711 | ||
2eda66f4 | 712 | static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags) |
5e23b923 | 713 | { |
155961e8 | 714 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
715 | struct urb *urb; |
716 | unsigned char *buf; | |
717 | unsigned int pipe; | |
718 | int err, size; | |
719 | ||
720 | BT_DBG("%s", hdev->name); | |
721 | ||
9bfa35fe MH |
722 | if (!data->intr_ep) |
723 | return -ENODEV; | |
724 | ||
2eda66f4 | 725 | urb = usb_alloc_urb(0, mem_flags); |
5e23b923 MH |
726 | if (!urb) |
727 | return -ENOMEM; | |
728 | ||
729 | size = le16_to_cpu(data->intr_ep->wMaxPacketSize); | |
730 | ||
2eda66f4 | 731 | buf = kmalloc(size, mem_flags); |
5e23b923 MH |
732 | if (!buf) { |
733 | usb_free_urb(urb); | |
734 | return -ENOMEM; | |
735 | } | |
736 | ||
737 | pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress); | |
738 | ||
739 | usb_fill_int_urb(urb, data->udev, pipe, buf, size, | |
89e7533d | 740 | btusb_intr_complete, hdev, data->intr_ep->bInterval); |
5e23b923 MH |
741 | |
742 | urb->transfer_flags |= URB_FREE_BUFFER; | |
743 | ||
744 | usb_anchor_urb(urb, &data->intr_anchor); | |
745 | ||
2eda66f4 | 746 | err = usb_submit_urb(urb, mem_flags); |
5e23b923 | 747 | if (err < 0) { |
d4b8d1c9 | 748 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
749 | bt_dev_err(hdev, "urb %p submission failed (%d)", |
750 | urb, -err); | |
5e23b923 | 751 | usb_unanchor_urb(urb); |
5e23b923 MH |
752 | } |
753 | ||
754 | usb_free_urb(urb); | |
755 | ||
756 | return err; | |
757 | } | |
758 | ||
759 | static void btusb_bulk_complete(struct urb *urb) | |
760 | { | |
761 | struct hci_dev *hdev = urb->context; | |
155961e8 | 762 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
763 | int err; |
764 | ||
89e7533d MH |
765 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, |
766 | urb->actual_length); | |
5e23b923 MH |
767 | |
768 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
769 | return; | |
770 | ||
771 | if (urb->status == 0) { | |
9bfa35fe MH |
772 | hdev->stat.byte_rx += urb->actual_length; |
773 | ||
2cbd3f5c | 774 | if (data->recv_bulk(data, urb->transfer_buffer, |
1ffa4ad0 | 775 | urb->actual_length) < 0) { |
2064ee33 | 776 | bt_dev_err(hdev, "corrupted ACL packet"); |
5e23b923 MH |
777 | hdev->stat.err_rx++; |
778 | } | |
85560c4a CC |
779 | } else if (urb->status == -ENOENT) { |
780 | /* Avoid suspend failed when usb_kill_urb */ | |
781 | return; | |
5e23b923 MH |
782 | } |
783 | ||
784 | if (!test_bit(BTUSB_BULK_RUNNING, &data->flags)) | |
785 | return; | |
786 | ||
787 | usb_anchor_urb(urb, &data->bulk_anchor); | |
652fd781 | 788 | usb_mark_last_busy(data->udev); |
5e23b923 MH |
789 | |
790 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
791 | if (err < 0) { | |
4935f1c1 | 792 | /* -EPERM: urb is being killed; |
d98422cb DR |
793 | * -ENODEV: device got disconnected |
794 | */ | |
4935f1c1 | 795 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
796 | bt_dev_err(hdev, "urb %p failed to resubmit (%d)", |
797 | urb, -err); | |
5e23b923 MH |
798 | usb_unanchor_urb(urb); |
799 | } | |
800 | } | |
801 | ||
2eda66f4 | 802 | static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags) |
5e23b923 | 803 | { |
155961e8 | 804 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
805 | struct urb *urb; |
806 | unsigned char *buf; | |
807 | unsigned int pipe; | |
290ba200 | 808 | int err, size = HCI_MAX_FRAME_SIZE; |
5e23b923 MH |
809 | |
810 | BT_DBG("%s", hdev->name); | |
811 | ||
9bfa35fe MH |
812 | if (!data->bulk_rx_ep) |
813 | return -ENODEV; | |
814 | ||
2eda66f4 | 815 | urb = usb_alloc_urb(0, mem_flags); |
5e23b923 MH |
816 | if (!urb) |
817 | return -ENOMEM; | |
818 | ||
2eda66f4 | 819 | buf = kmalloc(size, mem_flags); |
5e23b923 MH |
820 | if (!buf) { |
821 | usb_free_urb(urb); | |
822 | return -ENOMEM; | |
823 | } | |
824 | ||
825 | pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress); | |
826 | ||
89e7533d MH |
827 | usb_fill_bulk_urb(urb, data->udev, pipe, buf, size, |
828 | btusb_bulk_complete, hdev); | |
5e23b923 MH |
829 | |
830 | urb->transfer_flags |= URB_FREE_BUFFER; | |
831 | ||
7bee549e | 832 | usb_mark_last_busy(data->udev); |
5e23b923 MH |
833 | usb_anchor_urb(urb, &data->bulk_anchor); |
834 | ||
2eda66f4 | 835 | err = usb_submit_urb(urb, mem_flags); |
5e23b923 | 836 | if (err < 0) { |
d4b8d1c9 | 837 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
838 | bt_dev_err(hdev, "urb %p submission failed (%d)", |
839 | urb, -err); | |
5e23b923 | 840 | usb_unanchor_urb(urb); |
5e23b923 MH |
841 | } |
842 | ||
843 | usb_free_urb(urb); | |
844 | ||
845 | return err; | |
846 | } | |
847 | ||
9bfa35fe MH |
848 | static void btusb_isoc_complete(struct urb *urb) |
849 | { | |
850 | struct hci_dev *hdev = urb->context; | |
155961e8 | 851 | struct btusb_data *data = hci_get_drvdata(hdev); |
9bfa35fe MH |
852 | int i, err; |
853 | ||
89e7533d MH |
854 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, |
855 | urb->actual_length); | |
9bfa35fe MH |
856 | |
857 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
858 | return; | |
859 | ||
860 | if (urb->status == 0) { | |
861 | for (i = 0; i < urb->number_of_packets; i++) { | |
862 | unsigned int offset = urb->iso_frame_desc[i].offset; | |
863 | unsigned int length = urb->iso_frame_desc[i].actual_length; | |
864 | ||
865 | if (urb->iso_frame_desc[i].status) | |
866 | continue; | |
867 | ||
868 | hdev->stat.byte_rx += length; | |
869 | ||
1ffa4ad0 MH |
870 | if (btusb_recv_isoc(data, urb->transfer_buffer + offset, |
871 | length) < 0) { | |
2064ee33 | 872 | bt_dev_err(hdev, "corrupted SCO packet"); |
9bfa35fe MH |
873 | hdev->stat.err_rx++; |
874 | } | |
875 | } | |
85560c4a CC |
876 | } else if (urb->status == -ENOENT) { |
877 | /* Avoid suspend failed when usb_kill_urb */ | |
878 | return; | |
9bfa35fe MH |
879 | } |
880 | ||
881 | if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags)) | |
882 | return; | |
883 | ||
884 | usb_anchor_urb(urb, &data->isoc_anchor); | |
885 | ||
886 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
887 | if (err < 0) { | |
4935f1c1 | 888 | /* -EPERM: urb is being killed; |
d98422cb DR |
889 | * -ENODEV: device got disconnected |
890 | */ | |
4935f1c1 | 891 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
892 | bt_dev_err(hdev, "urb %p failed to resubmit (%d)", |
893 | urb, -err); | |
9bfa35fe MH |
894 | usb_unanchor_urb(urb); |
895 | } | |
896 | } | |
897 | ||
42b16b3f | 898 | static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu) |
9bfa35fe MH |
899 | { |
900 | int i, offset = 0; | |
901 | ||
902 | BT_DBG("len %d mtu %d", len, mtu); | |
903 | ||
904 | for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu; | |
905 | i++, offset += mtu, len -= mtu) { | |
906 | urb->iso_frame_desc[i].offset = offset; | |
907 | urb->iso_frame_desc[i].length = mtu; | |
908 | } | |
909 | ||
910 | if (len && i < BTUSB_MAX_ISOC_FRAMES) { | |
911 | urb->iso_frame_desc[i].offset = offset; | |
912 | urb->iso_frame_desc[i].length = len; | |
913 | i++; | |
914 | } | |
915 | ||
916 | urb->number_of_packets = i; | |
917 | } | |
918 | ||
2eda66f4 | 919 | static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags) |
9bfa35fe | 920 | { |
155961e8 | 921 | struct btusb_data *data = hci_get_drvdata(hdev); |
9bfa35fe MH |
922 | struct urb *urb; |
923 | unsigned char *buf; | |
924 | unsigned int pipe; | |
925 | int err, size; | |
926 | ||
927 | BT_DBG("%s", hdev->name); | |
928 | ||
929 | if (!data->isoc_rx_ep) | |
930 | return -ENODEV; | |
931 | ||
2eda66f4 | 932 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags); |
9bfa35fe MH |
933 | if (!urb) |
934 | return -ENOMEM; | |
935 | ||
936 | size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) * | |
937 | BTUSB_MAX_ISOC_FRAMES; | |
938 | ||
2eda66f4 | 939 | buf = kmalloc(size, mem_flags); |
9bfa35fe MH |
940 | if (!buf) { |
941 | usb_free_urb(urb); | |
942 | return -ENOMEM; | |
943 | } | |
944 | ||
945 | pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress); | |
946 | ||
fa0fb93f | 947 | usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete, |
89e7533d | 948 | hdev, data->isoc_rx_ep->bInterval); |
9bfa35fe | 949 | |
89e7533d | 950 | urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP; |
9bfa35fe MH |
951 | |
952 | __fill_isoc_descriptor(urb, size, | |
89e7533d | 953 | le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize)); |
9bfa35fe MH |
954 | |
955 | usb_anchor_urb(urb, &data->isoc_anchor); | |
956 | ||
2eda66f4 | 957 | err = usb_submit_urb(urb, mem_flags); |
9bfa35fe | 958 | if (err < 0) { |
d4b8d1c9 | 959 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
960 | bt_dev_err(hdev, "urb %p submission failed (%d)", |
961 | urb, -err); | |
9bfa35fe | 962 | usb_unanchor_urb(urb); |
9bfa35fe MH |
963 | } |
964 | ||
965 | usb_free_urb(urb); | |
966 | ||
967 | return err; | |
968 | } | |
969 | ||
9d08f504 MH |
970 | static void btusb_diag_complete(struct urb *urb) |
971 | { | |
972 | struct hci_dev *hdev = urb->context; | |
973 | struct btusb_data *data = hci_get_drvdata(hdev); | |
974 | int err; | |
975 | ||
976 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, | |
977 | urb->actual_length); | |
978 | ||
979 | if (urb->status == 0) { | |
980 | struct sk_buff *skb; | |
981 | ||
982 | skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC); | |
983 | if (skb) { | |
59ae1d12 JB |
984 | skb_put_data(skb, urb->transfer_buffer, |
985 | urb->actual_length); | |
9d08f504 MH |
986 | hci_recv_diag(hdev, skb); |
987 | } | |
988 | } else if (urb->status == -ENOENT) { | |
989 | /* Avoid suspend failed when usb_kill_urb */ | |
990 | return; | |
991 | } | |
992 | ||
993 | if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags)) | |
994 | return; | |
995 | ||
996 | usb_anchor_urb(urb, &data->diag_anchor); | |
997 | usb_mark_last_busy(data->udev); | |
998 | ||
999 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
1000 | if (err < 0) { | |
1001 | /* -EPERM: urb is being killed; | |
d98422cb DR |
1002 | * -ENODEV: device got disconnected |
1003 | */ | |
9d08f504 | 1004 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
1005 | bt_dev_err(hdev, "urb %p failed to resubmit (%d)", |
1006 | urb, -err); | |
9d08f504 MH |
1007 | usb_unanchor_urb(urb); |
1008 | } | |
1009 | } | |
1010 | ||
1011 | static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags) | |
1012 | { | |
1013 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1014 | struct urb *urb; | |
1015 | unsigned char *buf; | |
1016 | unsigned int pipe; | |
1017 | int err, size = HCI_MAX_FRAME_SIZE; | |
1018 | ||
1019 | BT_DBG("%s", hdev->name); | |
1020 | ||
1021 | if (!data->diag_rx_ep) | |
1022 | return -ENODEV; | |
1023 | ||
1024 | urb = usb_alloc_urb(0, mem_flags); | |
1025 | if (!urb) | |
1026 | return -ENOMEM; | |
1027 | ||
1028 | buf = kmalloc(size, mem_flags); | |
1029 | if (!buf) { | |
1030 | usb_free_urb(urb); | |
1031 | return -ENOMEM; | |
1032 | } | |
1033 | ||
1034 | pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress); | |
1035 | ||
1036 | usb_fill_bulk_urb(urb, data->udev, pipe, buf, size, | |
1037 | btusb_diag_complete, hdev); | |
1038 | ||
1039 | urb->transfer_flags |= URB_FREE_BUFFER; | |
1040 | ||
1041 | usb_mark_last_busy(data->udev); | |
1042 | usb_anchor_urb(urb, &data->diag_anchor); | |
1043 | ||
1044 | err = usb_submit_urb(urb, mem_flags); | |
1045 | if (err < 0) { | |
1046 | if (err != -EPERM && err != -ENODEV) | |
2064ee33 MH |
1047 | bt_dev_err(hdev, "urb %p submission failed (%d)", |
1048 | urb, -err); | |
9d08f504 MH |
1049 | usb_unanchor_urb(urb); |
1050 | } | |
1051 | ||
1052 | usb_free_urb(urb); | |
1053 | ||
1054 | return err; | |
1055 | } | |
1056 | ||
5e23b923 | 1057 | static void btusb_tx_complete(struct urb *urb) |
7bee549e ON |
1058 | { |
1059 | struct sk_buff *skb = urb->context; | |
89e7533d | 1060 | struct hci_dev *hdev = (struct hci_dev *)skb->dev; |
155961e8 | 1061 | struct btusb_data *data = hci_get_drvdata(hdev); |
7bee549e | 1062 | |
89e7533d MH |
1063 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, |
1064 | urb->actual_length); | |
7bee549e ON |
1065 | |
1066 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
1067 | goto done; | |
1068 | ||
1069 | if (!urb->status) | |
1070 | hdev->stat.byte_tx += urb->transfer_buffer_length; | |
1071 | else | |
1072 | hdev->stat.err_tx++; | |
1073 | ||
1074 | done: | |
1075 | spin_lock(&data->txlock); | |
1076 | data->tx_in_flight--; | |
1077 | spin_unlock(&data->txlock); | |
1078 | ||
1079 | kfree(urb->setup_packet); | |
1080 | ||
1081 | kfree_skb(skb); | |
1082 | } | |
1083 | ||
1084 | static void btusb_isoc_tx_complete(struct urb *urb) | |
5e23b923 MH |
1085 | { |
1086 | struct sk_buff *skb = urb->context; | |
89e7533d | 1087 | struct hci_dev *hdev = (struct hci_dev *)skb->dev; |
5e23b923 | 1088 | |
89e7533d MH |
1089 | BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status, |
1090 | urb->actual_length); | |
5e23b923 MH |
1091 | |
1092 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
1093 | goto done; | |
1094 | ||
1095 | if (!urb->status) | |
1096 | hdev->stat.byte_tx += urb->transfer_buffer_length; | |
1097 | else | |
1098 | hdev->stat.err_tx++; | |
1099 | ||
1100 | done: | |
1101 | kfree(urb->setup_packet); | |
1102 | ||
1103 | kfree_skb(skb); | |
1104 | } | |
1105 | ||
1106 | static int btusb_open(struct hci_dev *hdev) | |
1107 | { | |
155961e8 | 1108 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
1109 | int err; |
1110 | ||
1111 | BT_DBG("%s", hdev->name); | |
1112 | ||
c7e163fe EH |
1113 | err = usb_autopm_get_interface(data->intf); |
1114 | if (err < 0) | |
1115 | return err; | |
1116 | ||
ace31982 KBYT |
1117 | /* Patching USB firmware files prior to starting any URBs of HCI path |
1118 | * It is more safe to use USB bulk channel for downloading USB patch | |
1119 | */ | |
1120 | if (data->setup_on_usb) { | |
1121 | err = data->setup_on_usb(hdev); | |
eb50042f | 1122 | if (err < 0) |
ace31982 KBYT |
1123 | return err; |
1124 | } | |
1125 | ||
7bee549e | 1126 | data->intf->needs_remote_wakeup = 1; |
a0085f25 SG |
1127 | /* device specific wakeup source enabled and required for USB |
1128 | * remote wakeup while host is suspended | |
1129 | */ | |
1130 | device_wakeup_enable(&data->udev->dev); | |
7bee549e | 1131 | |
5e23b923 | 1132 | if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) |
7bee549e | 1133 | goto done; |
5e23b923 | 1134 | |
2eda66f4 | 1135 | err = btusb_submit_intr_urb(hdev, GFP_KERNEL); |
43c2e57f MH |
1136 | if (err < 0) |
1137 | goto failed; | |
1138 | ||
1139 | err = btusb_submit_bulk_urb(hdev, GFP_KERNEL); | |
5e23b923 | 1140 | if (err < 0) { |
43c2e57f MH |
1141 | usb_kill_anchored_urbs(&data->intr_anchor); |
1142 | goto failed; | |
5e23b923 MH |
1143 | } |
1144 | ||
43c2e57f MH |
1145 | set_bit(BTUSB_BULK_RUNNING, &data->flags); |
1146 | btusb_submit_bulk_urb(hdev, GFP_KERNEL); | |
1147 | ||
9d08f504 MH |
1148 | if (data->diag) { |
1149 | if (!btusb_submit_diag_urb(hdev, GFP_KERNEL)) | |
1150 | set_bit(BTUSB_DIAG_RUNNING, &data->flags); | |
1151 | } | |
1152 | ||
7bee549e ON |
1153 | done: |
1154 | usb_autopm_put_interface(data->intf); | |
43c2e57f MH |
1155 | return 0; |
1156 | ||
1157 | failed: | |
1158 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); | |
7bee549e | 1159 | usb_autopm_put_interface(data->intf); |
5e23b923 MH |
1160 | return err; |
1161 | } | |
1162 | ||
7bee549e ON |
1163 | static void btusb_stop_traffic(struct btusb_data *data) |
1164 | { | |
1165 | usb_kill_anchored_urbs(&data->intr_anchor); | |
1166 | usb_kill_anchored_urbs(&data->bulk_anchor); | |
1167 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
9d08f504 | 1168 | usb_kill_anchored_urbs(&data->diag_anchor); |
7bee549e ON |
1169 | } |
1170 | ||
5e23b923 MH |
1171 | static int btusb_close(struct hci_dev *hdev) |
1172 | { | |
155961e8 | 1173 | struct btusb_data *data = hci_get_drvdata(hdev); |
7bee549e | 1174 | int err; |
5e23b923 MH |
1175 | |
1176 | BT_DBG("%s", hdev->name); | |
1177 | ||
e8c3c3d2 | 1178 | cancel_work_sync(&data->work); |
404291ac | 1179 | cancel_work_sync(&data->waker); |
e8c3c3d2 | 1180 | |
9bfa35fe | 1181 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
5e23b923 | 1182 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); |
5e23b923 | 1183 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); |
9d08f504 | 1184 | clear_bit(BTUSB_DIAG_RUNNING, &data->flags); |
7bee549e ON |
1185 | |
1186 | btusb_stop_traffic(data); | |
803b5836 MH |
1187 | btusb_free_frags(data); |
1188 | ||
7bee549e ON |
1189 | err = usb_autopm_get_interface(data->intf); |
1190 | if (err < 0) | |
7b8e2c1d | 1191 | goto failed; |
7bee549e ON |
1192 | |
1193 | data->intf->needs_remote_wakeup = 0; | |
a0085f25 | 1194 | device_wakeup_disable(&data->udev->dev); |
7bee549e | 1195 | usb_autopm_put_interface(data->intf); |
5e23b923 | 1196 | |
7b8e2c1d ON |
1197 | failed: |
1198 | usb_scuttle_anchored_urbs(&data->deferred); | |
5e23b923 MH |
1199 | return 0; |
1200 | } | |
1201 | ||
1202 | static int btusb_flush(struct hci_dev *hdev) | |
1203 | { | |
155961e8 | 1204 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
1205 | |
1206 | BT_DBG("%s", hdev->name); | |
1207 | ||
1208 | usb_kill_anchored_urbs(&data->tx_anchor); | |
803b5836 | 1209 | btusb_free_frags(data); |
5e23b923 MH |
1210 | |
1211 | return 0; | |
1212 | } | |
1213 | ||
047b2ec8 | 1214 | static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb) |
5e23b923 | 1215 | { |
155961e8 | 1216 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
1217 | struct usb_ctrlrequest *dr; |
1218 | struct urb *urb; | |
1219 | unsigned int pipe; | |
5e23b923 | 1220 | |
047b2ec8 MH |
1221 | urb = usb_alloc_urb(0, GFP_KERNEL); |
1222 | if (!urb) | |
1223 | return ERR_PTR(-ENOMEM); | |
5e23b923 | 1224 | |
047b2ec8 MH |
1225 | dr = kmalloc(sizeof(*dr), GFP_KERNEL); |
1226 | if (!dr) { | |
1227 | usb_free_urb(urb); | |
1228 | return ERR_PTR(-ENOMEM); | |
1229 | } | |
5e23b923 | 1230 | |
047b2ec8 | 1231 | dr->bRequestType = data->cmdreq_type; |
893ba544 | 1232 | dr->bRequest = data->cmdreq; |
047b2ec8 MH |
1233 | dr->wIndex = 0; |
1234 | dr->wValue = 0; | |
1235 | dr->wLength = __cpu_to_le16(skb->len); | |
7bd8f09f | 1236 | |
047b2ec8 | 1237 | pipe = usb_sndctrlpipe(data->udev, 0x00); |
5e23b923 | 1238 | |
89e7533d | 1239 | usb_fill_control_urb(urb, data->udev, pipe, (void *)dr, |
047b2ec8 | 1240 | skb->data, skb->len, btusb_tx_complete, skb); |
5e23b923 | 1241 | |
89e7533d | 1242 | skb->dev = (void *)hdev; |
5e23b923 | 1243 | |
047b2ec8 MH |
1244 | return urb; |
1245 | } | |
5e23b923 | 1246 | |
047b2ec8 MH |
1247 | static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb) |
1248 | { | |
1249 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1250 | struct urb *urb; | |
1251 | unsigned int pipe; | |
5e23b923 | 1252 | |
047b2ec8 MH |
1253 | if (!data->bulk_tx_ep) |
1254 | return ERR_PTR(-ENODEV); | |
9bfa35fe | 1255 | |
047b2ec8 MH |
1256 | urb = usb_alloc_urb(0, GFP_KERNEL); |
1257 | if (!urb) | |
1258 | return ERR_PTR(-ENOMEM); | |
5e23b923 | 1259 | |
047b2ec8 | 1260 | pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress); |
5e23b923 | 1261 | |
047b2ec8 MH |
1262 | usb_fill_bulk_urb(urb, data->udev, pipe, |
1263 | skb->data, skb->len, btusb_tx_complete, skb); | |
5e23b923 | 1264 | |
89e7533d | 1265 | skb->dev = (void *)hdev; |
5e23b923 | 1266 | |
047b2ec8 MH |
1267 | return urb; |
1268 | } | |
9bfa35fe | 1269 | |
047b2ec8 MH |
1270 | static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb) |
1271 | { | |
1272 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1273 | struct urb *urb; | |
1274 | unsigned int pipe; | |
9bfa35fe | 1275 | |
047b2ec8 MH |
1276 | if (!data->isoc_tx_ep) |
1277 | return ERR_PTR(-ENODEV); | |
9bfa35fe | 1278 | |
047b2ec8 MH |
1279 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL); |
1280 | if (!urb) | |
1281 | return ERR_PTR(-ENOMEM); | |
9bfa35fe | 1282 | |
047b2ec8 | 1283 | pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress); |
9bfa35fe | 1284 | |
047b2ec8 MH |
1285 | usb_fill_int_urb(urb, data->udev, pipe, |
1286 | skb->data, skb->len, btusb_isoc_tx_complete, | |
1287 | skb, data->isoc_tx_ep->bInterval); | |
9bfa35fe | 1288 | |
047b2ec8 | 1289 | urb->transfer_flags = URB_ISO_ASAP; |
5e23b923 | 1290 | |
047b2ec8 MH |
1291 | __fill_isoc_descriptor(urb, skb->len, |
1292 | le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); | |
5e23b923 | 1293 | |
89e7533d | 1294 | skb->dev = (void *)hdev; |
047b2ec8 MH |
1295 | |
1296 | return urb; | |
1297 | } | |
1298 | ||
1299 | static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb) | |
1300 | { | |
1301 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1302 | int err; | |
7bee549e | 1303 | |
5e23b923 MH |
1304 | usb_anchor_urb(urb, &data->tx_anchor); |
1305 | ||
e9753eff | 1306 | err = usb_submit_urb(urb, GFP_KERNEL); |
5e23b923 | 1307 | if (err < 0) { |
5a9b80e2 | 1308 | if (err != -EPERM && err != -ENODEV) |
2064ee33 MH |
1309 | bt_dev_err(hdev, "urb %p submission failed (%d)", |
1310 | urb, -err); | |
5e23b923 MH |
1311 | kfree(urb->setup_packet); |
1312 | usb_unanchor_urb(urb); | |
7bee549e ON |
1313 | } else { |
1314 | usb_mark_last_busy(data->udev); | |
5e23b923 MH |
1315 | } |
1316 | ||
54a8a79c | 1317 | usb_free_urb(urb); |
5e23b923 MH |
1318 | return err; |
1319 | } | |
1320 | ||
047b2ec8 MH |
1321 | static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb) |
1322 | { | |
1323 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1324 | unsigned long flags; | |
1325 | bool suspending; | |
1326 | ||
1327 | spin_lock_irqsave(&data->txlock, flags); | |
1328 | suspending = test_bit(BTUSB_SUSPENDING, &data->flags); | |
1329 | if (!suspending) | |
1330 | data->tx_in_flight++; | |
1331 | spin_unlock_irqrestore(&data->txlock, flags); | |
1332 | ||
1333 | if (!suspending) | |
1334 | return submit_tx_urb(hdev, urb); | |
1335 | ||
1336 | usb_anchor_urb(urb, &data->deferred); | |
1337 | schedule_work(&data->waker); | |
1338 | ||
1339 | usb_free_urb(urb); | |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb) | |
1344 | { | |
1345 | struct urb *urb; | |
1346 | ||
1347 | BT_DBG("%s", hdev->name); | |
1348 | ||
618e8bc2 | 1349 | switch (hci_skb_pkt_type(skb)) { |
047b2ec8 MH |
1350 | case HCI_COMMAND_PKT: |
1351 | urb = alloc_ctrl_urb(hdev, skb); | |
1352 | if (IS_ERR(urb)) | |
1353 | return PTR_ERR(urb); | |
1354 | ||
1355 | hdev->stat.cmd_tx++; | |
1356 | return submit_or_queue_tx_urb(hdev, urb); | |
1357 | ||
1358 | case HCI_ACLDATA_PKT: | |
1359 | urb = alloc_bulk_urb(hdev, skb); | |
1360 | if (IS_ERR(urb)) | |
1361 | return PTR_ERR(urb); | |
1362 | ||
1363 | hdev->stat.acl_tx++; | |
1364 | return submit_or_queue_tx_urb(hdev, urb); | |
1365 | ||
1366 | case HCI_SCODATA_PKT: | |
1367 | if (hci_conn_num(hdev, SCO_LINK) < 1) | |
1368 | return -ENODEV; | |
1369 | ||
1370 | urb = alloc_isoc_urb(hdev, skb); | |
1371 | if (IS_ERR(urb)) | |
1372 | return PTR_ERR(urb); | |
1373 | ||
1374 | hdev->stat.sco_tx++; | |
1375 | return submit_tx_urb(hdev, urb); | |
1376 | } | |
1377 | ||
1378 | return -EILSEQ; | |
1379 | } | |
1380 | ||
5e23b923 MH |
1381 | static void btusb_notify(struct hci_dev *hdev, unsigned int evt) |
1382 | { | |
155961e8 | 1383 | struct btusb_data *data = hci_get_drvdata(hdev); |
5e23b923 MH |
1384 | |
1385 | BT_DBG("%s evt %d", hdev->name, evt); | |
1386 | ||
014f7bc7 MH |
1387 | if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) { |
1388 | data->sco_num = hci_conn_num(hdev, SCO_LINK); | |
43c2e57f | 1389 | schedule_work(&data->work); |
a780efa8 | 1390 | } |
5e23b923 MH |
1391 | } |
1392 | ||
42b16b3f | 1393 | static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting) |
9bfa35fe | 1394 | { |
155961e8 | 1395 | struct btusb_data *data = hci_get_drvdata(hdev); |
9bfa35fe MH |
1396 | struct usb_interface *intf = data->isoc; |
1397 | struct usb_endpoint_descriptor *ep_desc; | |
1398 | int i, err; | |
1399 | ||
1400 | if (!data->isoc) | |
1401 | return -ENODEV; | |
1402 | ||
459232fc | 1403 | err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting); |
9bfa35fe | 1404 | if (err < 0) { |
2064ee33 | 1405 | bt_dev_err(hdev, "setting interface failed (%d)", -err); |
9bfa35fe MH |
1406 | return err; |
1407 | } | |
1408 | ||
1409 | data->isoc_altsetting = altsetting; | |
1410 | ||
1411 | data->isoc_tx_ep = NULL; | |
1412 | data->isoc_rx_ep = NULL; | |
1413 | ||
1414 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | |
1415 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; | |
1416 | ||
1417 | if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) { | |
1418 | data->isoc_tx_ep = ep_desc; | |
1419 | continue; | |
1420 | } | |
1421 | ||
1422 | if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) { | |
1423 | data->isoc_rx_ep = ep_desc; | |
1424 | continue; | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | if (!data->isoc_tx_ep || !data->isoc_rx_ep) { | |
2064ee33 | 1429 | bt_dev_err(hdev, "invalid SCO descriptors"); |
9bfa35fe MH |
1430 | return -ENODEV; |
1431 | } | |
1432 | ||
1433 | return 0; | |
1434 | } | |
1435 | ||
5e23b923 MH |
1436 | static void btusb_work(struct work_struct *work) |
1437 | { | |
1438 | struct btusb_data *data = container_of(work, struct btusb_data, work); | |
1439 | struct hci_dev *hdev = data->hdev; | |
f4001d28 | 1440 | int new_alts; |
7bee549e | 1441 | int err; |
5e23b923 | 1442 | |
014f7bc7 | 1443 | if (data->sco_num > 0) { |
08b8b6c4 | 1444 | if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) { |
8efdd0cd | 1445 | err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf); |
7bee549e ON |
1446 | if (err < 0) { |
1447 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
1448 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
1449 | return; | |
1450 | } | |
1451 | ||
08b8b6c4 | 1452 | set_bit(BTUSB_DID_ISO_RESUME, &data->flags); |
7bee549e | 1453 | } |
f4001d28 MA |
1454 | |
1455 | if (hdev->voice_setting & 0x0020) { | |
1456 | static const int alts[3] = { 2, 4, 5 }; | |
89e7533d | 1457 | |
014f7bc7 | 1458 | new_alts = alts[data->sco_num - 1]; |
f4001d28 | 1459 | } else { |
014f7bc7 | 1460 | new_alts = data->sco_num; |
f4001d28 MA |
1461 | } |
1462 | ||
1463 | if (data->isoc_altsetting != new_alts) { | |
f6fc86f2 KP |
1464 | unsigned long flags; |
1465 | ||
9bfa35fe MH |
1466 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
1467 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
1468 | ||
8f9d02f4 KP |
1469 | /* When isochronous alternate setting needs to be |
1470 | * changed, because SCO connection has been added | |
1471 | * or removed, a packet fragment may be left in the | |
1472 | * reassembling state. This could lead to wrongly | |
1473 | * assembled fragments. | |
1474 | * | |
1475 | * Clear outstanding fragment when selecting a new | |
1476 | * alternate setting. | |
1477 | */ | |
f6fc86f2 | 1478 | spin_lock_irqsave(&data->rxlock, flags); |
8f9d02f4 KP |
1479 | kfree_skb(data->sco_skb); |
1480 | data->sco_skb = NULL; | |
f6fc86f2 | 1481 | spin_unlock_irqrestore(&data->rxlock, flags); |
8f9d02f4 | 1482 | |
f4001d28 | 1483 | if (__set_isoc_interface(hdev, new_alts) < 0) |
9bfa35fe MH |
1484 | return; |
1485 | } | |
1486 | ||
1487 | if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) { | |
2eda66f4 | 1488 | if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0) |
9bfa35fe MH |
1489 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
1490 | else | |
2eda66f4 | 1491 | btusb_submit_isoc_urb(hdev, GFP_KERNEL); |
9bfa35fe MH |
1492 | } |
1493 | } else { | |
1494 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
1495 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
1496 | ||
1497 | __set_isoc_interface(hdev, 0); | |
08b8b6c4 | 1498 | if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags)) |
8efdd0cd | 1499 | usb_autopm_put_interface(data->isoc ? data->isoc : data->intf); |
5e23b923 MH |
1500 | } |
1501 | } | |
1502 | ||
7bee549e ON |
1503 | static void btusb_waker(struct work_struct *work) |
1504 | { | |
1505 | struct btusb_data *data = container_of(work, struct btusb_data, waker); | |
1506 | int err; | |
1507 | ||
1508 | err = usb_autopm_get_interface(data->intf); | |
1509 | if (err < 0) | |
1510 | return; | |
1511 | ||
1512 | usb_autopm_put_interface(data->intf); | |
1513 | } | |
1514 | ||
9f8f962c MH |
1515 | static int btusb_setup_bcm92035(struct hci_dev *hdev) |
1516 | { | |
1517 | struct sk_buff *skb; | |
1518 | u8 val = 0x00; | |
1519 | ||
1520 | BT_DBG("%s", hdev->name); | |
1521 | ||
1522 | skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT); | |
1523 | if (IS_ERR(skb)) | |
2064ee33 | 1524 | bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb)); |
9f8f962c MH |
1525 | else |
1526 | kfree_skb(skb); | |
1527 | ||
1528 | return 0; | |
1529 | } | |
1530 | ||
81cac64b MH |
1531 | static int btusb_setup_csr(struct hci_dev *hdev) |
1532 | { | |
1533 | struct hci_rp_read_local_version *rp; | |
1534 | struct sk_buff *skb; | |
81cac64b MH |
1535 | |
1536 | BT_DBG("%s", hdev->name); | |
1537 | ||
7cd84d72 MH |
1538 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, |
1539 | HCI_INIT_TIMEOUT); | |
1540 | if (IS_ERR(skb)) { | |
1541 | int err = PTR_ERR(skb); | |
2064ee33 | 1542 | bt_dev_err(hdev, "CSR: Local version failed (%d)", err); |
7cd84d72 MH |
1543 | return err; |
1544 | } | |
1545 | ||
1546 | if (skb->len != sizeof(struct hci_rp_read_local_version)) { | |
2064ee33 | 1547 | bt_dev_err(hdev, "CSR: Local version length mismatch"); |
7cd84d72 MH |
1548 | kfree_skb(skb); |
1549 | return -EIO; | |
1550 | } | |
81cac64b | 1551 | |
89e7533d | 1552 | rp = (struct hci_rp_read_local_version *)skb->data; |
81cac64b | 1553 | |
6cafcd95 JH |
1554 | /* Detect controllers which aren't real CSR ones. */ |
1555 | if (le16_to_cpu(rp->manufacturer) != 10 || | |
1556 | le16_to_cpu(rp->lmp_subver) == 0x0c5c) { | |
9641d343 MH |
1557 | /* Clear the reset quirk since this is not an actual |
1558 | * early Bluetooth 1.1 device from CSR. | |
1559 | */ | |
1560 | clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); | |
81cac64b | 1561 | |
9641d343 MH |
1562 | /* These fake CSR controllers have all a broken |
1563 | * stored link key handling and so just disable it. | |
1564 | */ | |
1565 | set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks); | |
1566 | } | |
81cac64b MH |
1567 | |
1568 | kfree_skb(skb); | |
1569 | ||
9641d343 | 1570 | return 0; |
81cac64b MH |
1571 | } |
1572 | ||
dffd30ee | 1573 | static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev, |
89e7533d | 1574 | struct intel_version *ver) |
dffd30ee THJA |
1575 | { |
1576 | const struct firmware *fw; | |
1577 | char fwname[64]; | |
1578 | int ret; | |
1579 | ||
1580 | snprintf(fwname, sizeof(fwname), | |
1581 | "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq", | |
1582 | ver->hw_platform, ver->hw_variant, ver->hw_revision, | |
1583 | ver->fw_variant, ver->fw_revision, ver->fw_build_num, | |
1584 | ver->fw_build_ww, ver->fw_build_yy); | |
1585 | ||
1586 | ret = request_firmware(&fw, fwname, &hdev->dev); | |
1587 | if (ret < 0) { | |
1588 | if (ret == -EINVAL) { | |
1589 | BT_ERR("%s Intel firmware file request failed (%d)", | |
1590 | hdev->name, ret); | |
1591 | return NULL; | |
1592 | } | |
1593 | ||
1594 | BT_ERR("%s failed to open Intel firmware file: %s(%d)", | |
1595 | hdev->name, fwname, ret); | |
1596 | ||
1597 | /* If the correct firmware patch file is not found, use the | |
1598 | * default firmware patch file instead | |
1599 | */ | |
1600 | snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq", | |
1601 | ver->hw_platform, ver->hw_variant); | |
1602 | if (request_firmware(&fw, fwname, &hdev->dev) < 0) { | |
1603 | BT_ERR("%s failed to open default Intel fw file: %s", | |
1604 | hdev->name, fwname); | |
1605 | return NULL; | |
1606 | } | |
1607 | } | |
1608 | ||
2064ee33 | 1609 | bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname); |
dffd30ee THJA |
1610 | |
1611 | return fw; | |
1612 | } | |
1613 | ||
1614 | static int btusb_setup_intel_patching(struct hci_dev *hdev, | |
1615 | const struct firmware *fw, | |
1616 | const u8 **fw_ptr, int *disable_patch) | |
1617 | { | |
1618 | struct sk_buff *skb; | |
1619 | struct hci_command_hdr *cmd; | |
1620 | const u8 *cmd_param; | |
1621 | struct hci_event_hdr *evt = NULL; | |
1622 | const u8 *evt_param = NULL; | |
1623 | int remain = fw->size - (*fw_ptr - fw->data); | |
1624 | ||
1625 | /* The first byte indicates the types of the patch command or event. | |
1626 | * 0x01 means HCI command and 0x02 is HCI event. If the first bytes | |
1627 | * in the current firmware buffer doesn't start with 0x01 or | |
1628 | * the size of remain buffer is smaller than HCI command header, | |
1629 | * the firmware file is corrupted and it should stop the patching | |
1630 | * process. | |
1631 | */ | |
1632 | if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) { | |
1633 | BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name); | |
1634 | return -EINVAL; | |
1635 | } | |
1636 | (*fw_ptr)++; | |
1637 | remain--; | |
1638 | ||
1639 | cmd = (struct hci_command_hdr *)(*fw_ptr); | |
1640 | *fw_ptr += sizeof(*cmd); | |
1641 | remain -= sizeof(*cmd); | |
1642 | ||
1643 | /* Ensure that the remain firmware data is long enough than the length | |
1644 | * of command parameter. If not, the firmware file is corrupted. | |
1645 | */ | |
1646 | if (remain < cmd->plen) { | |
1647 | BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name); | |
1648 | return -EFAULT; | |
1649 | } | |
1650 | ||
1651 | /* If there is a command that loads a patch in the firmware | |
1652 | * file, then enable the patch upon success, otherwise just | |
1653 | * disable the manufacturer mode, for example patch activation | |
1654 | * is not required when the default firmware patch file is used | |
1655 | * because there are no patch data to load. | |
1656 | */ | |
1657 | if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e) | |
1658 | *disable_patch = 0; | |
1659 | ||
1660 | cmd_param = *fw_ptr; | |
1661 | *fw_ptr += cmd->plen; | |
1662 | remain -= cmd->plen; | |
1663 | ||
1664 | /* This reads the expected events when the above command is sent to the | |
1665 | * device. Some vendor commands expects more than one events, for | |
1666 | * example command status event followed by vendor specific event. | |
1667 | * For this case, it only keeps the last expected event. so the command | |
1668 | * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of | |
1669 | * last expected event. | |
1670 | */ | |
1671 | while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) { | |
1672 | (*fw_ptr)++; | |
1673 | remain--; | |
1674 | ||
1675 | evt = (struct hci_event_hdr *)(*fw_ptr); | |
1676 | *fw_ptr += sizeof(*evt); | |
1677 | remain -= sizeof(*evt); | |
1678 | ||
1679 | if (remain < evt->plen) { | |
1680 | BT_ERR("%s Intel fw corrupted: invalid evt len", | |
1681 | hdev->name); | |
1682 | return -EFAULT; | |
1683 | } | |
1684 | ||
1685 | evt_param = *fw_ptr; | |
1686 | *fw_ptr += evt->plen; | |
1687 | remain -= evt->plen; | |
1688 | } | |
1689 | ||
1690 | /* Every HCI commands in the firmware file has its correspond event. | |
1691 | * If event is not found or remain is smaller than zero, the firmware | |
1692 | * file is corrupted. | |
1693 | */ | |
1694 | if (!evt || !evt_param || remain < 0) { | |
1695 | BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name); | |
1696 | return -EFAULT; | |
1697 | } | |
1698 | ||
1699 | skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen, | |
1700 | cmd_param, evt->evt, HCI_INIT_TIMEOUT); | |
1701 | if (IS_ERR(skb)) { | |
1702 | BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)", | |
1703 | hdev->name, cmd->opcode, PTR_ERR(skb)); | |
d9c78e97 | 1704 | return PTR_ERR(skb); |
dffd30ee THJA |
1705 | } |
1706 | ||
1707 | /* It ensures that the returned event matches the event data read from | |
1708 | * the firmware file. At fist, it checks the length and then | |
1709 | * the contents of the event. | |
1710 | */ | |
1711 | if (skb->len != evt->plen) { | |
1712 | BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name, | |
1713 | le16_to_cpu(cmd->opcode)); | |
1714 | kfree_skb(skb); | |
1715 | return -EFAULT; | |
1716 | } | |
1717 | ||
1718 | if (memcmp(skb->data, evt_param, evt->plen)) { | |
1719 | BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)", | |
1720 | hdev->name, le16_to_cpu(cmd->opcode)); | |
1721 | kfree_skb(skb); | |
1722 | return -EFAULT; | |
1723 | } | |
1724 | kfree_skb(skb); | |
1725 | ||
1726 | return 0; | |
1727 | } | |
1728 | ||
1729 | static int btusb_setup_intel(struct hci_dev *hdev) | |
1730 | { | |
1731 | struct sk_buff *skb; | |
1732 | const struct firmware *fw; | |
1733 | const u8 *fw_ptr; | |
28dc4b92 | 1734 | int disable_patch, err; |
6c483de1 | 1735 | struct intel_version ver; |
dffd30ee | 1736 | |
dffd30ee THJA |
1737 | BT_DBG("%s", hdev->name); |
1738 | ||
1739 | /* The controller has a bug with the first HCI command sent to it | |
1740 | * returning number of completed commands as zero. This would stall the | |
1741 | * command processing in the Bluetooth core. | |
1742 | * | |
1743 | * As a workaround, send HCI Reset command first which will reset the | |
1744 | * number of completed commands and allow normal command processing | |
1745 | * from now on. | |
1746 | */ | |
1747 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); | |
1748 | if (IS_ERR(skb)) { | |
1749 | BT_ERR("%s sending initial HCI reset command failed (%ld)", | |
1750 | hdev->name, PTR_ERR(skb)); | |
d9c78e97 | 1751 | return PTR_ERR(skb); |
dffd30ee THJA |
1752 | } |
1753 | kfree_skb(skb); | |
1754 | ||
1755 | /* Read Intel specific controller version first to allow selection of | |
1756 | * which firmware file to load. | |
1757 | * | |
1758 | * The returned information are hardware variant and revision plus | |
1759 | * firmware variant, revision and build number. | |
1760 | */ | |
6c483de1 LP |
1761 | err = btintel_read_version(hdev, &ver); |
1762 | if (err) | |
1763 | return err; | |
dffd30ee | 1764 | |
2064ee33 MH |
1765 | bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x", |
1766 | ver.hw_platform, ver.hw_variant, ver.hw_revision, | |
1767 | ver.fw_variant, ver.fw_revision, ver.fw_build_num, | |
1768 | ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num); | |
dffd30ee THJA |
1769 | |
1770 | /* fw_patch_num indicates the version of patch the device currently | |
1771 | * have. If there is no patch data in the device, it is always 0x00. | |
5075edae | 1772 | * So, if it is other than 0x00, no need to patch the device again. |
dffd30ee | 1773 | */ |
6c483de1 | 1774 | if (ver.fw_patch_num) { |
2064ee33 MH |
1775 | bt_dev_info(hdev, "Intel device is already patched. " |
1776 | "patch num: %02x", ver.fw_patch_num); | |
213445b2 | 1777 | goto complete; |
dffd30ee THJA |
1778 | } |
1779 | ||
1780 | /* Opens the firmware patch file based on the firmware version read | |
1781 | * from the controller. If it fails to open the matching firmware | |
1782 | * patch file, it tries to open the default firmware patch file. | |
1783 | * If no patch file is found, allow the device to operate without | |
1784 | * a patch. | |
1785 | */ | |
6c483de1 LP |
1786 | fw = btusb_setup_intel_get_fw(hdev, &ver); |
1787 | if (!fw) | |
213445b2 | 1788 | goto complete; |
dffd30ee THJA |
1789 | fw_ptr = fw->data; |
1790 | ||
28dc4b92 | 1791 | /* Enable the manufacturer mode of the controller. |
dffd30ee THJA |
1792 | * Only while this mode is enabled, the driver can download the |
1793 | * firmware patch data and configuration parameters. | |
1794 | */ | |
28dc4b92 LP |
1795 | err = btintel_enter_mfg(hdev); |
1796 | if (err) { | |
dffd30ee | 1797 | release_firmware(fw); |
28dc4b92 | 1798 | return err; |
dffd30ee THJA |
1799 | } |
1800 | ||
dffd30ee THJA |
1801 | disable_patch = 1; |
1802 | ||
1803 | /* The firmware data file consists of list of Intel specific HCI | |
1804 | * commands and its expected events. The first byte indicates the | |
1805 | * type of the message, either HCI command or HCI event. | |
1806 | * | |
1807 | * It reads the command and its expected event from the firmware file, | |
1808 | * and send to the controller. Once __hci_cmd_sync_ev() returns, | |
1809 | * the returned event is compared with the event read from the firmware | |
1810 | * file and it will continue until all the messages are downloaded to | |
1811 | * the controller. | |
1812 | * | |
1813 | * Once the firmware patching is completed successfully, | |
1814 | * the manufacturer mode is disabled with reset and activating the | |
1815 | * downloaded patch. | |
1816 | * | |
1817 | * If the firmware patching fails, the manufacturer mode is | |
1818 | * disabled with reset and deactivating the patch. | |
1819 | * | |
1820 | * If the default patch file is used, no reset is done when disabling | |
1821 | * the manufacturer. | |
1822 | */ | |
1823 | while (fw->size > fw_ptr - fw->data) { | |
1824 | int ret; | |
1825 | ||
1826 | ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr, | |
1827 | &disable_patch); | |
1828 | if (ret < 0) | |
1829 | goto exit_mfg_deactivate; | |
1830 | } | |
1831 | ||
1832 | release_firmware(fw); | |
1833 | ||
1834 | if (disable_patch) | |
1835 | goto exit_mfg_disable; | |
1836 | ||
1837 | /* Patching completed successfully and disable the manufacturer mode | |
1838 | * with reset and activate the downloaded firmware patches. | |
1839 | */ | |
28dc4b92 LP |
1840 | err = btintel_exit_mfg(hdev, true, true); |
1841 | if (err) | |
1842 | return err; | |
dffd30ee | 1843 | |
2064ee33 | 1844 | bt_dev_info(hdev, "Intel firmware patch completed and activated"); |
dffd30ee | 1845 | |
213445b2 | 1846 | goto complete; |
dffd30ee THJA |
1847 | |
1848 | exit_mfg_disable: | |
1849 | /* Disable the manufacturer mode without reset */ | |
28dc4b92 LP |
1850 | err = btintel_exit_mfg(hdev, false, false); |
1851 | if (err) | |
1852 | return err; | |
dffd30ee | 1853 | |
2064ee33 | 1854 | bt_dev_info(hdev, "Intel firmware patch completed"); |
40cb0984 | 1855 | |
213445b2 | 1856 | goto complete; |
dffd30ee THJA |
1857 | |
1858 | exit_mfg_deactivate: | |
1859 | release_firmware(fw); | |
1860 | ||
1861 | /* Patching failed. Disable the manufacturer mode with reset and | |
1862 | * deactivate the downloaded firmware patches. | |
1863 | */ | |
28dc4b92 LP |
1864 | err = btintel_exit_mfg(hdev, true, false); |
1865 | if (err) | |
1866 | return err; | |
dffd30ee | 1867 | |
2064ee33 | 1868 | bt_dev_info(hdev, "Intel firmware patch completed and deactivated"); |
dffd30ee | 1869 | |
213445b2 MH |
1870 | complete: |
1871 | /* Set the event mask for Intel specific vendor events. This enables | |
1872 | * a few extra events that are useful during general operation. | |
1873 | */ | |
1874 | btintel_set_event_mask_mfg(hdev, false); | |
1875 | ||
4185a0f5 | 1876 | btintel_check_bdaddr(hdev); |
dffd30ee THJA |
1877 | return 0; |
1878 | } | |
1879 | ||
cda0dd78 MH |
1880 | static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode) |
1881 | { | |
1882 | struct sk_buff *skb; | |
1883 | struct hci_event_hdr *hdr; | |
1884 | struct hci_ev_cmd_complete *evt; | |
1885 | ||
1886 | skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC); | |
1887 | if (!skb) | |
1888 | return -ENOMEM; | |
1889 | ||
4df864c1 | 1890 | hdr = skb_put(skb, sizeof(*hdr)); |
cda0dd78 MH |
1891 | hdr->evt = HCI_EV_CMD_COMPLETE; |
1892 | hdr->plen = sizeof(*evt) + 1; | |
1893 | ||
4df864c1 | 1894 | evt = skb_put(skb, sizeof(*evt)); |
cda0dd78 MH |
1895 | evt->ncmd = 0x01; |
1896 | evt->opcode = cpu_to_le16(opcode); | |
1897 | ||
634fef61 | 1898 | skb_put_u8(skb, 0x00); |
cda0dd78 | 1899 | |
618e8bc2 | 1900 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; |
cda0dd78 MH |
1901 | |
1902 | return hci_recv_frame(hdev, skb); | |
1903 | } | |
1904 | ||
1905 | static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer, | |
1906 | int count) | |
1907 | { | |
1908 | /* When the device is in bootloader mode, then it can send | |
1909 | * events via the bulk endpoint. These events are treated the | |
1910 | * same way as the ones received from the interrupt endpoint. | |
1911 | */ | |
1912 | if (test_bit(BTUSB_BOOTLOADER, &data->flags)) | |
1913 | return btusb_recv_intr(data, buffer, count); | |
1914 | ||
1915 | return btusb_recv_bulk(data, buffer, count); | |
1916 | } | |
1917 | ||
ccd6da2a MH |
1918 | static void btusb_intel_bootup(struct btusb_data *data, const void *ptr, |
1919 | unsigned int len) | |
1920 | { | |
1921 | const struct intel_bootup *evt = ptr; | |
1922 | ||
1923 | if (len != sizeof(*evt)) | |
1924 | return; | |
1925 | ||
1926 | if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) { | |
1927 | smp_mb__after_atomic(); | |
1928 | wake_up_bit(&data->flags, BTUSB_BOOTING); | |
1929 | } | |
1930 | } | |
1931 | ||
1932 | static void btusb_intel_secure_send_result(struct btusb_data *data, | |
1933 | const void *ptr, unsigned int len) | |
1934 | { | |
1935 | const struct intel_secure_send_result *evt = ptr; | |
1936 | ||
1937 | if (len != sizeof(*evt)) | |
1938 | return; | |
1939 | ||
1940 | if (evt->result) | |
1941 | set_bit(BTUSB_FIRMWARE_FAILED, &data->flags); | |
1942 | ||
1943 | if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) && | |
1944 | test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) { | |
1945 | smp_mb__after_atomic(); | |
1946 | wake_up_bit(&data->flags, BTUSB_DOWNLOADING); | |
1947 | } | |
1948 | } | |
1949 | ||
cda0dd78 MH |
1950 | static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb) |
1951 | { | |
1952 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1953 | ||
1954 | if (test_bit(BTUSB_BOOTLOADER, &data->flags)) { | |
1955 | struct hci_event_hdr *hdr = (void *)skb->data; | |
1956 | ||
ccd6da2a MH |
1957 | if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff && |
1958 | hdr->plen > 0) { | |
1959 | const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1; | |
1960 | unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1; | |
1961 | ||
1962 | switch (skb->data[2]) { | |
1963 | case 0x02: | |
1964 | /* When switching to the operational firmware | |
1965 | * the device sends a vendor specific event | |
1966 | * indicating that the bootup completed. | |
1967 | */ | |
1968 | btusb_intel_bootup(data, ptr, len); | |
1969 | break; | |
1970 | case 0x06: | |
1971 | /* When the firmware loading completes the | |
1972 | * device sends out a vendor specific event | |
1973 | * indicating the result of the firmware | |
1974 | * loading. | |
1975 | */ | |
1976 | btusb_intel_secure_send_result(data, ptr, len); | |
1977 | break; | |
fad70972 | 1978 | } |
cda0dd78 MH |
1979 | } |
1980 | } | |
1981 | ||
1982 | return hci_recv_frame(hdev, skb); | |
1983 | } | |
1984 | ||
1985 | static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb) | |
1986 | { | |
1987 | struct btusb_data *data = hci_get_drvdata(hdev); | |
1988 | struct urb *urb; | |
1989 | ||
1990 | BT_DBG("%s", hdev->name); | |
1991 | ||
618e8bc2 | 1992 | switch (hci_skb_pkt_type(skb)) { |
cda0dd78 MH |
1993 | case HCI_COMMAND_PKT: |
1994 | if (test_bit(BTUSB_BOOTLOADER, &data->flags)) { | |
1995 | struct hci_command_hdr *cmd = (void *)skb->data; | |
1996 | __u16 opcode = le16_to_cpu(cmd->opcode); | |
1997 | ||
1998 | /* When in bootloader mode and the command 0xfc09 | |
1999 | * is received, it needs to be send down the | |
2000 | * bulk endpoint. So allocate a bulk URB instead. | |
2001 | */ | |
2002 | if (opcode == 0xfc09) | |
2003 | urb = alloc_bulk_urb(hdev, skb); | |
2004 | else | |
2005 | urb = alloc_ctrl_urb(hdev, skb); | |
2006 | ||
2007 | /* When the 0xfc01 command is issued to boot into | |
2008 | * the operational firmware, it will actually not | |
2009 | * send a command complete event. To keep the flow | |
2010 | * control working inject that event here. | |
2011 | */ | |
2012 | if (opcode == 0xfc01) | |
2013 | inject_cmd_complete(hdev, opcode); | |
2014 | } else { | |
2015 | urb = alloc_ctrl_urb(hdev, skb); | |
2016 | } | |
2017 | if (IS_ERR(urb)) | |
2018 | return PTR_ERR(urb); | |
2019 | ||
2020 | hdev->stat.cmd_tx++; | |
2021 | return submit_or_queue_tx_urb(hdev, urb); | |
2022 | ||
2023 | case HCI_ACLDATA_PKT: | |
2024 | urb = alloc_bulk_urb(hdev, skb); | |
2025 | if (IS_ERR(urb)) | |
2026 | return PTR_ERR(urb); | |
2027 | ||
2028 | hdev->stat.acl_tx++; | |
2029 | return submit_or_queue_tx_urb(hdev, urb); | |
2030 | ||
2031 | case HCI_SCODATA_PKT: | |
2032 | if (hci_conn_num(hdev, SCO_LINK) < 1) | |
2033 | return -ENODEV; | |
2034 | ||
2035 | urb = alloc_isoc_urb(hdev, skb); | |
2036 | if (IS_ERR(urb)) | |
2037 | return PTR_ERR(urb); | |
2038 | ||
2039 | hdev->stat.sco_tx++; | |
2040 | return submit_tx_urb(hdev, urb); | |
2041 | } | |
2042 | ||
2043 | return -EILSEQ; | |
2044 | } | |
2045 | ||
424443b7 RH |
2046 | static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver, |
2047 | struct intel_boot_params *params, | |
2048 | char *fw_name, size_t len, | |
2049 | const char *suffix) | |
2050 | { | |
2051 | switch (ver->hw_variant) { | |
2052 | case 0x0b: /* SfP */ | |
2053 | case 0x0c: /* WsP */ | |
2054 | snprintf(fw_name, len, "intel/ibt-%u-%u.%s", | |
2055 | le16_to_cpu(ver->hw_variant), | |
2056 | le16_to_cpu(params->dev_revid), | |
2057 | suffix); | |
2058 | break; | |
2059 | case 0x11: /* JfP */ | |
2060 | case 0x12: /* ThP */ | |
2061 | case 0x13: /* HrP */ | |
2062 | case 0x14: /* CcP */ | |
2063 | snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s", | |
2064 | le16_to_cpu(ver->hw_variant), | |
2065 | le16_to_cpu(ver->hw_revision), | |
2066 | le16_to_cpu(ver->fw_revision), | |
2067 | suffix); | |
2068 | break; | |
2069 | default: | |
2070 | return false; | |
2071 | } | |
2072 | return true; | |
2073 | } | |
2074 | ||
cda0dd78 MH |
2075 | static int btusb_setup_intel_new(struct hci_dev *hdev) |
2076 | { | |
2077 | static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01, | |
2078 | 0x00, 0x08, 0x04, 0x00 }; | |
2079 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2080 | struct sk_buff *skb; | |
6c483de1 | 2081 | struct intel_version ver; |
cda0dd78 MH |
2082 | struct intel_boot_params *params; |
2083 | const struct firmware *fw; | |
2084 | const u8 *fw_ptr; | |
e66890a9 | 2085 | u32 frag_len; |
cda0dd78 MH |
2086 | char fwname[64]; |
2087 | ktime_t calltime, delta, rettime; | |
2088 | unsigned long long duration; | |
2089 | int err; | |
2090 | ||
2091 | BT_DBG("%s", hdev->name); | |
2092 | ||
2093 | calltime = ktime_get(); | |
2094 | ||
2095 | /* Read the Intel version information to determine if the device | |
2096 | * is in bootloader mode or if it already has operational firmware | |
2097 | * loaded. | |
2098 | */ | |
6c483de1 LP |
2099 | err = btintel_read_version(hdev, &ver); |
2100 | if (err) | |
2101 | return err; | |
cda0dd78 MH |
2102 | |
2103 | /* The hardware platform number has a fixed value of 0x37 and | |
2104 | * for now only accept this single value. | |
2105 | */ | |
6c483de1 | 2106 | if (ver.hw_platform != 0x37) { |
cda0dd78 | 2107 | BT_ERR("%s: Unsupported Intel hardware platform (%u)", |
6c483de1 | 2108 | hdev->name, ver.hw_platform); |
cda0dd78 MH |
2109 | return -EINVAL; |
2110 | } | |
2111 | ||
9268834b THJA |
2112 | /* Check for supported iBT hardware variants of this firmware |
2113 | * loading method. | |
a0af53b5 THJA |
2114 | * |
2115 | * This check has been put in place to ensure correct forward | |
2116 | * compatibility options when newer hardware variants come along. | |
cda0dd78 | 2117 | */ |
9268834b THJA |
2118 | switch (ver.hw_variant) { |
2119 | case 0x0b: /* SfP */ | |
2120 | case 0x0c: /* WsP */ | |
86a6129a | 2121 | case 0x11: /* JfP */ |
de766142 | 2122 | case 0x12: /* ThP */ |
d6608ee5 | 2123 | case 0x13: /* HrP */ |
424443b7 | 2124 | case 0x14: /* CcP */ |
9268834b THJA |
2125 | break; |
2126 | default: | |
cda0dd78 | 2127 | BT_ERR("%s: Unsupported Intel hardware variant (%u)", |
6c483de1 | 2128 | hdev->name, ver.hw_variant); |
cda0dd78 MH |
2129 | return -EINVAL; |
2130 | } | |
2131 | ||
6c483de1 | 2132 | btintel_version_info(hdev, &ver); |
cda0dd78 MH |
2133 | |
2134 | /* The firmware variant determines if the device is in bootloader | |
2135 | * mode or is running operational firmware. The value 0x06 identifies | |
2136 | * the bootloader and the value 0x23 identifies the operational | |
2137 | * firmware. | |
2138 | * | |
2139 | * When the operational firmware is already present, then only | |
2140 | * the check for valid Bluetooth device address is needed. This | |
2141 | * determines if the device will be added as configured or | |
2142 | * unconfigured controller. | |
2143 | * | |
2144 | * It is not possible to use the Secure Boot Parameters in this | |
2145 | * case since that command is only available in bootloader mode. | |
2146 | */ | |
6c483de1 | 2147 | if (ver.fw_variant == 0x23) { |
cda0dd78 | 2148 | clear_bit(BTUSB_BOOTLOADER, &data->flags); |
4185a0f5 | 2149 | btintel_check_bdaddr(hdev); |
cda0dd78 MH |
2150 | return 0; |
2151 | } | |
2152 | ||
2153 | /* If the device is not in bootloader mode, then the only possible | |
2154 | * choice is to return an error and abort the device initialization. | |
2155 | */ | |
6c483de1 | 2156 | if (ver.fw_variant != 0x06) { |
cda0dd78 | 2157 | BT_ERR("%s: Unsupported Intel firmware variant (%u)", |
6c483de1 | 2158 | hdev->name, ver.fw_variant); |
cda0dd78 MH |
2159 | return -ENODEV; |
2160 | } | |
2161 | ||
cda0dd78 MH |
2162 | /* Read the secure boot parameters to identify the operating |
2163 | * details of the bootloader. | |
2164 | */ | |
2165 | skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT); | |
2166 | if (IS_ERR(skb)) { | |
2167 | BT_ERR("%s: Reading Intel boot parameters failed (%ld)", | |
2168 | hdev->name, PTR_ERR(skb)); | |
2169 | return PTR_ERR(skb); | |
2170 | } | |
2171 | ||
2172 | if (skb->len != sizeof(*params)) { | |
2173 | BT_ERR("%s: Intel boot parameters size mismatch", hdev->name); | |
2174 | kfree_skb(skb); | |
2175 | return -EILSEQ; | |
2176 | } | |
2177 | ||
2178 | params = (struct intel_boot_params *)skb->data; | |
cda0dd78 | 2179 | |
2064ee33 MH |
2180 | bt_dev_info(hdev, "Device revision is %u", |
2181 | le16_to_cpu(params->dev_revid)); | |
cda0dd78 | 2182 | |
2064ee33 MH |
2183 | bt_dev_info(hdev, "Secure boot is %s", |
2184 | params->secure_boot ? "enabled" : "disabled"); | |
cda0dd78 | 2185 | |
2064ee33 MH |
2186 | bt_dev_info(hdev, "OTP lock is %s", |
2187 | params->otp_lock ? "enabled" : "disabled"); | |
2220994e | 2188 | |
2064ee33 MH |
2189 | bt_dev_info(hdev, "API lock is %s", |
2190 | params->api_lock ? "enabled" : "disabled"); | |
2220994e | 2191 | |
2064ee33 MH |
2192 | bt_dev_info(hdev, "Debug lock is %s", |
2193 | params->debug_lock ? "enabled" : "disabled"); | |
2220994e | 2194 | |
2064ee33 MH |
2195 | bt_dev_info(hdev, "Minimum firmware build %u week %u %u", |
2196 | params->min_fw_build_nn, params->min_fw_build_cw, | |
2197 | 2000 + params->min_fw_build_yy); | |
cda0dd78 MH |
2198 | |
2199 | /* It is required that every single firmware fragment is acknowledged | |
2200 | * with a command complete event. If the boot parameters indicate | |
2201 | * that this bootloader does not send them, then abort the setup. | |
2202 | */ | |
2203 | if (params->limited_cce != 0x00) { | |
2204 | BT_ERR("%s: Unsupported Intel firmware loading method (%u)", | |
2205 | hdev->name, params->limited_cce); | |
2206 | kfree_skb(skb); | |
2207 | return -EINVAL; | |
2208 | } | |
2209 | ||
2210 | /* If the OTP has no valid Bluetooth device address, then there will | |
2211 | * also be no valid address for the operational firmware. | |
2212 | */ | |
2213 | if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) { | |
2064ee33 | 2214 | bt_dev_info(hdev, "No device address configured"); |
cda0dd78 MH |
2215 | set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); |
2216 | } | |
2217 | ||
2218 | /* With this Intel bootloader only the hardware variant and device | |
af3715e5 J |
2219 | * revision information are used to select the right firmware for SfP |
2220 | * and WsP. | |
cda0dd78 | 2221 | * |
230b04ac THJA |
2222 | * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi. |
2223 | * | |
2224 | * Currently the supported hardware variants are: | |
2225 | * 11 (0x0b) for iBT3.0 (LnP/SfP) | |
2226 | * 12 (0x0c) for iBT3.5 (WsP) | |
af3715e5 J |
2227 | * |
2228 | * For ThP/JfP and for future SKU's, the FW name varies based on HW | |
2229 | * variant, HW revision and FW revision, as these are dependent on CNVi | |
2230 | * and RF Combination. | |
2231 | * | |
86a6129a THJA |
2232 | * 17 (0x11) for iBT3.5 (JfP) |
2233 | * 18 (0x12) for iBT3.5 (ThP) | |
af3715e5 J |
2234 | * |
2235 | * The firmware file name for these will be | |
2236 | * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi. | |
2237 | * | |
cda0dd78 | 2238 | */ |
424443b7 RH |
2239 | err = btusb_setup_intel_new_get_fw_name(&ver, params, fwname, |
2240 | sizeof(fwname), "sfi"); | |
2241 | if (!err) { | |
af3715e5 J |
2242 | BT_ERR("%s: Unsupported Intel firmware naming", hdev->name); |
2243 | return -EINVAL; | |
2244 | } | |
cda0dd78 MH |
2245 | |
2246 | err = request_firmware(&fw, fwname, &hdev->dev); | |
2247 | if (err < 0) { | |
2248 | BT_ERR("%s: Failed to load Intel firmware file (%d)", | |
2249 | hdev->name, err); | |
2250 | kfree_skb(skb); | |
2251 | return err; | |
2252 | } | |
2253 | ||
2064ee33 | 2254 | bt_dev_info(hdev, "Found device firmware: %s", fwname); |
cda0dd78 | 2255 | |
52cc9168 THJA |
2256 | /* Save the DDC file name for later use to apply once the firmware |
2257 | * downloading is done. | |
2258 | */ | |
424443b7 RH |
2259 | err = btusb_setup_intel_new_get_fw_name(&ver, params, fwname, |
2260 | sizeof(fwname), "ddc"); | |
2261 | if (!err) { | |
af3715e5 J |
2262 | BT_ERR("%s: Unsupported Intel firmware naming", hdev->name); |
2263 | return -EINVAL; | |
2264 | } | |
52cc9168 | 2265 | |
cda0dd78 MH |
2266 | kfree_skb(skb); |
2267 | ||
2268 | if (fw->size < 644) { | |
2269 | BT_ERR("%s: Invalid size of firmware file (%zu)", | |
2270 | hdev->name, fw->size); | |
2271 | err = -EBADF; | |
2272 | goto done; | |
2273 | } | |
2274 | ||
2275 | set_bit(BTUSB_DOWNLOADING, &data->flags); | |
2276 | ||
2277 | /* Start the firmware download transaction with the Init fragment | |
2278 | * represented by the 128 bytes of CSS header. | |
2279 | */ | |
09df123d | 2280 | err = btintel_secure_send(hdev, 0x00, 128, fw->data); |
cda0dd78 MH |
2281 | if (err < 0) { |
2282 | BT_ERR("%s: Failed to send firmware header (%d)", | |
2283 | hdev->name, err); | |
2284 | goto done; | |
2285 | } | |
2286 | ||
2287 | /* Send the 256 bytes of public key information from the firmware | |
2288 | * as the PKey fragment. | |
2289 | */ | |
09df123d | 2290 | err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128); |
cda0dd78 MH |
2291 | if (err < 0) { |
2292 | BT_ERR("%s: Failed to send firmware public key (%d)", | |
2293 | hdev->name, err); | |
2294 | goto done; | |
2295 | } | |
2296 | ||
2297 | /* Send the 256 bytes of signature information from the firmware | |
2298 | * as the Sign fragment. | |
2299 | */ | |
09df123d | 2300 | err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388); |
cda0dd78 MH |
2301 | if (err < 0) { |
2302 | BT_ERR("%s: Failed to send firmware signature (%d)", | |
2303 | hdev->name, err); | |
2304 | goto done; | |
2305 | } | |
2306 | ||
2307 | fw_ptr = fw->data + 644; | |
e66890a9 | 2308 | frag_len = 0; |
cda0dd78 MH |
2309 | |
2310 | while (fw_ptr - fw->data < fw->size) { | |
e66890a9 | 2311 | struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len); |
cda0dd78 | 2312 | |
e66890a9 | 2313 | frag_len += sizeof(*cmd) + cmd->plen; |
cda0dd78 | 2314 | |
5075edae | 2315 | /* The parameter length of the secure send command requires |
e66890a9 MH |
2316 | * a 4 byte alignment. It happens so that the firmware file |
2317 | * contains proper Intel_NOP commands to align the fragments | |
2318 | * as needed. | |
2319 | * | |
2320 | * Send set of commands with 4 byte alignment from the | |
2321 | * firmware data buffer as a single Data fragement. | |
cda0dd78 | 2322 | */ |
e66890a9 | 2323 | if (!(frag_len % 4)) { |
09df123d | 2324 | err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr); |
e66890a9 MH |
2325 | if (err < 0) { |
2326 | BT_ERR("%s: Failed to send firmware data (%d)", | |
2327 | hdev->name, err); | |
2328 | goto done; | |
2329 | } | |
cda0dd78 | 2330 | |
e66890a9 MH |
2331 | fw_ptr += frag_len; |
2332 | frag_len = 0; | |
2333 | } | |
cda0dd78 MH |
2334 | } |
2335 | ||
ce6bb929 MH |
2336 | set_bit(BTUSB_FIRMWARE_LOADED, &data->flags); |
2337 | ||
2064ee33 | 2338 | bt_dev_info(hdev, "Waiting for firmware download to complete"); |
a087a98e | 2339 | |
cda0dd78 MH |
2340 | /* Before switching the device into operational mode and with that |
2341 | * booting the loaded firmware, wait for the bootloader notification | |
2342 | * that all fragments have been successfully received. | |
2343 | * | |
a087a98e JH |
2344 | * When the event processing receives the notification, then the |
2345 | * BTUSB_DOWNLOADING flag will be cleared. | |
2346 | * | |
2347 | * The firmware loading should not take longer than 5 seconds | |
2348 | * and thus just timeout if that happens and fail the setup | |
2349 | * of this device. | |
cda0dd78 | 2350 | */ |
129a7693 JH |
2351 | err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING, |
2352 | TASK_INTERRUPTIBLE, | |
2353 | msecs_to_jiffies(5000)); | |
f0a70a04 | 2354 | if (err == -EINTR) { |
a087a98e | 2355 | BT_ERR("%s: Firmware loading interrupted", hdev->name); |
a087a98e JH |
2356 | goto done; |
2357 | } | |
cda0dd78 | 2358 | |
a087a98e JH |
2359 | if (err) { |
2360 | BT_ERR("%s: Firmware loading timeout", hdev->name); | |
2361 | err = -ETIMEDOUT; | |
2362 | goto done; | |
cda0dd78 MH |
2363 | } |
2364 | ||
2365 | if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) { | |
2366 | BT_ERR("%s: Firmware loading failed", hdev->name); | |
2367 | err = -ENOEXEC; | |
2368 | goto done; | |
2369 | } | |
2370 | ||
2371 | rettime = ktime_get(); | |
2372 | delta = ktime_sub(rettime, calltime); | |
2373 | duration = (unsigned long long) ktime_to_ns(delta) >> 10; | |
2374 | ||
2064ee33 | 2375 | bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration); |
cda0dd78 MH |
2376 | |
2377 | done: | |
2378 | release_firmware(fw); | |
2379 | ||
2380 | if (err < 0) | |
2381 | return err; | |
2382 | ||
2383 | calltime = ktime_get(); | |
2384 | ||
2385 | set_bit(BTUSB_BOOTING, &data->flags); | |
2386 | ||
2387 | skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param, | |
2388 | HCI_INIT_TIMEOUT); | |
2389 | if (IS_ERR(skb)) | |
2390 | return PTR_ERR(skb); | |
2391 | ||
2392 | kfree_skb(skb); | |
2393 | ||
2394 | /* The bootloader will not indicate when the device is ready. This | |
2395 | * is done by the operational firmware sending bootup notification. | |
fad70972 JH |
2396 | * |
2397 | * Booting into operational firmware should not take longer than | |
2398 | * 1 second. However if that happens, then just fail the setup | |
2399 | * since something went wrong. | |
cda0dd78 | 2400 | */ |
2064ee33 | 2401 | bt_dev_info(hdev, "Waiting for device to boot"); |
cda0dd78 | 2402 | |
129a7693 JH |
2403 | err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING, |
2404 | TASK_INTERRUPTIBLE, | |
2405 | msecs_to_jiffies(1000)); | |
cda0dd78 | 2406 | |
f0a70a04 | 2407 | if (err == -EINTR) { |
fad70972 JH |
2408 | BT_ERR("%s: Device boot interrupted", hdev->name); |
2409 | return -EINTR; | |
2410 | } | |
cda0dd78 | 2411 | |
fad70972 JH |
2412 | if (err) { |
2413 | BT_ERR("%s: Device boot timeout", hdev->name); | |
2414 | return -ETIMEDOUT; | |
cda0dd78 MH |
2415 | } |
2416 | ||
2417 | rettime = ktime_get(); | |
2418 | delta = ktime_sub(rettime, calltime); | |
2419 | duration = (unsigned long long) ktime_to_ns(delta) >> 10; | |
2420 | ||
2064ee33 | 2421 | bt_dev_info(hdev, "Device booted in %llu usecs", duration); |
cda0dd78 MH |
2422 | |
2423 | clear_bit(BTUSB_BOOTLOADER, &data->flags); | |
2424 | ||
52cc9168 THJA |
2425 | /* Once the device is running in operational mode, it needs to apply |
2426 | * the device configuration (DDC) parameters. | |
2427 | * | |
2428 | * The device can work without DDC parameters, so even if it fails | |
2429 | * to load the file, no need to fail the setup. | |
2430 | */ | |
e924d3d6 | 2431 | btintel_load_ddc_config(hdev, fwname); |
52cc9168 | 2432 | |
213445b2 MH |
2433 | /* Set the event mask for Intel specific vendor events. This enables |
2434 | * a few extra events that are useful during general operation. It | |
2435 | * does not enable any debugging related events. | |
2436 | * | |
2437 | * The device will function correctly without these events enabled | |
2438 | * and thus no need to fail the setup. | |
2439 | */ | |
2440 | btintel_set_event_mask(hdev, false); | |
2441 | ||
cda0dd78 MH |
2442 | return 0; |
2443 | } | |
2444 | ||
bfbd45e9 THJA |
2445 | static int btusb_shutdown_intel(struct hci_dev *hdev) |
2446 | { | |
2447 | struct sk_buff *skb; | |
2448 | long ret; | |
2449 | ||
2450 | /* Some platforms have an issue with BT LED when the interface is | |
2451 | * down or BT radio is turned off, which takes 5 seconds to BT LED | |
2452 | * goes off. This command turns off the BT LED immediately. | |
2453 | */ | |
2454 | skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT); | |
2455 | if (IS_ERR(skb)) { | |
2456 | ret = PTR_ERR(skb); | |
2457 | BT_ERR("%s: turning off Intel device LED failed (%ld)", | |
2458 | hdev->name, ret); | |
2459 | return ret; | |
2460 | } | |
2461 | kfree_skb(skb); | |
2462 | ||
2463 | return 0; | |
2464 | } | |
2465 | ||
a4ccc9e3 RJ |
2466 | #ifdef CONFIG_PM |
2467 | /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */ | |
2468 | static int marvell_config_oob_wake(struct hci_dev *hdev) | |
2469 | { | |
2470 | struct sk_buff *skb; | |
2471 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2472 | struct device *dev = &data->udev->dev; | |
2473 | u16 pin, gap, opcode; | |
2474 | int ret; | |
2475 | u8 cmd[5]; | |
2476 | ||
2477 | /* Move on if no wakeup pin specified */ | |
2478 | if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) || | |
2479 | of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap)) | |
2480 | return 0; | |
2481 | ||
2482 | /* Vendor specific command to configure a GPIO as wake-up pin */ | |
2483 | opcode = hci_opcode_pack(0x3F, 0x59); | |
2484 | cmd[0] = opcode & 0xFF; | |
2485 | cmd[1] = opcode >> 8; | |
2486 | cmd[2] = 2; /* length of parameters that follow */ | |
2487 | cmd[3] = pin; | |
2488 | cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */ | |
2489 | ||
2490 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); | |
2491 | if (!skb) { | |
2492 | bt_dev_err(hdev, "%s: No memory\n", __func__); | |
2493 | return -ENOMEM; | |
2494 | } | |
2495 | ||
59ae1d12 | 2496 | skb_put_data(skb, cmd, sizeof(cmd)); |
a4ccc9e3 RJ |
2497 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
2498 | ||
2499 | ret = btusb_send_frame(hdev, skb); | |
2500 | if (ret) { | |
2501 | bt_dev_err(hdev, "%s: configuration failed\n", __func__); | |
2502 | kfree_skb(skb); | |
2503 | return ret; | |
2504 | } | |
2505 | ||
2506 | return 0; | |
2507 | } | |
2508 | #endif | |
2509 | ||
ae8df494 AK |
2510 | static int btusb_set_bdaddr_marvell(struct hci_dev *hdev, |
2511 | const bdaddr_t *bdaddr) | |
2512 | { | |
2513 | struct sk_buff *skb; | |
2514 | u8 buf[8]; | |
2515 | long ret; | |
2516 | ||
2517 | buf[0] = 0xfe; | |
2518 | buf[1] = sizeof(bdaddr_t); | |
2519 | memcpy(buf + 2, bdaddr, sizeof(bdaddr_t)); | |
2520 | ||
2521 | skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT); | |
2522 | if (IS_ERR(skb)) { | |
2523 | ret = PTR_ERR(skb); | |
2064ee33 MH |
2524 | bt_dev_err(hdev, "changing Marvell device address failed (%ld)", |
2525 | ret); | |
ae8df494 AK |
2526 | return ret; |
2527 | } | |
2528 | kfree_skb(skb); | |
2529 | ||
2530 | return 0; | |
2531 | } | |
2532 | ||
84845ceb WJS |
2533 | #define BTUSB_EDGE_LED_COMMAND 0xfc77 |
2534 | ||
2535 | static void btusb_edge_set_led(struct hci_dev *hdev, bool state) | |
2536 | { | |
2537 | struct sk_buff *skb; | |
2538 | u8 config_led[] = { 0x09, 0x00, 0x01, 0x01 }; | |
2539 | ||
2540 | if (state) | |
2541 | config_led[1] = 0x01; | |
2542 | ||
2543 | skb = __hci_cmd_sync(hdev, BTUSB_EDGE_LED_COMMAND, sizeof(config_led), config_led, HCI_INIT_TIMEOUT); | |
2544 | if (IS_ERR(skb)) | |
2545 | BT_ERR("%s fail to set LED (%ld)", hdev->name, PTR_ERR(skb)); | |
2546 | else | |
2547 | kfree_skb(skb); | |
2548 | } | |
2549 | ||
2550 | static int btusb_edge_post_init(struct hci_dev *hdev) | |
2551 | { | |
2552 | btusb_edge_set_led(hdev, true); | |
2553 | return 0; | |
2554 | } | |
2555 | ||
2556 | static int btusb_edge_shutdown(struct hci_dev *hdev) | |
2557 | { | |
2558 | btusb_edge_set_led(hdev, false); | |
2559 | return 0; | |
2560 | } | |
2561 | ||
5859223e TK |
2562 | static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev, |
2563 | const bdaddr_t *bdaddr) | |
2564 | { | |
2565 | struct sk_buff *skb; | |
2566 | u8 buf[10]; | |
2567 | long ret; | |
2568 | ||
2569 | buf[0] = 0x01; | |
2570 | buf[1] = 0x01; | |
2571 | buf[2] = 0x00; | |
2572 | buf[3] = sizeof(bdaddr_t); | |
2573 | memcpy(buf + 4, bdaddr, sizeof(bdaddr_t)); | |
2574 | ||
2575 | skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT); | |
2576 | if (IS_ERR(skb)) { | |
2577 | ret = PTR_ERR(skb); | |
2064ee33 | 2578 | bt_dev_err(hdev, "Change address command failed (%ld)", ret); |
5859223e TK |
2579 | return ret; |
2580 | } | |
2581 | kfree_skb(skb); | |
2582 | ||
2583 | return 0; | |
2584 | } | |
2585 | ||
3267c884 KBYT |
2586 | #define QCA_DFU_PACKET_LEN 4096 |
2587 | ||
2588 | #define QCA_GET_TARGET_VERSION 0x09 | |
2589 | #define QCA_CHECK_STATUS 0x05 | |
2590 | #define QCA_DFU_DOWNLOAD 0x01 | |
2591 | ||
2592 | #define QCA_SYSCFG_UPDATED 0x40 | |
2593 | #define QCA_PATCH_UPDATED 0x80 | |
2594 | #define QCA_DFU_TIMEOUT 3000 | |
2595 | ||
2596 | struct qca_version { | |
2597 | __le32 rom_version; | |
2598 | __le32 patch_version; | |
2599 | __le32 ram_version; | |
2600 | __le32 ref_clock; | |
2601 | __u8 reserved[4]; | |
2602 | } __packed; | |
2603 | ||
2604 | struct qca_rampatch_version { | |
2605 | __le16 rom_version; | |
2606 | __le16 patch_version; | |
2607 | } __packed; | |
2608 | ||
2609 | struct qca_device_info { | |
bf906b3d KBYT |
2610 | u32 rom_version; |
2611 | u8 rampatch_hdr; /* length of header in rampatch */ | |
2612 | u8 nvm_hdr; /* length of header in NVM */ | |
2613 | u8 ver_offset; /* offset of version structure in rampatch */ | |
3267c884 KBYT |
2614 | }; |
2615 | ||
2616 | static const struct qca_device_info qca_devices_table[] = { | |
2617 | { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */ | |
2618 | { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */ | |
7f6e6363 | 2619 | { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */ |
3267c884 KBYT |
2620 | { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */ |
2621 | { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */ | |
2622 | { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */ | |
2623 | }; | |
2624 | ||
e68fc8b7 | 2625 | static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request, |
3267c884 KBYT |
2626 | void *data, u16 size) |
2627 | { | |
3267c884 KBYT |
2628 | int pipe, err; |
2629 | u8 *buf; | |
2630 | ||
2631 | buf = kmalloc(size, GFP_KERNEL); | |
2632 | if (!buf) | |
2633 | return -ENOMEM; | |
2634 | ||
2635 | /* Found some of USB hosts have IOT issues with ours so that we should | |
2636 | * not wait until HCI layer is ready. | |
2637 | */ | |
2638 | pipe = usb_rcvctrlpipe(udev, 0); | |
2639 | err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN, | |
2640 | 0, 0, buf, size, USB_CTRL_SET_TIMEOUT); | |
2641 | if (err < 0) { | |
e68fc8b7 | 2642 | dev_err(&udev->dev, "Failed to access otp area (%d)", err); |
3267c884 KBYT |
2643 | goto done; |
2644 | } | |
2645 | ||
2646 | memcpy(data, buf, size); | |
2647 | ||
2648 | done: | |
2649 | kfree(buf); | |
2650 | ||
2651 | return err; | |
2652 | } | |
2653 | ||
2654 | static int btusb_setup_qca_download_fw(struct hci_dev *hdev, | |
2655 | const struct firmware *firmware, | |
2656 | size_t hdr_size) | |
2657 | { | |
2658 | struct btusb_data *btdata = hci_get_drvdata(hdev); | |
2659 | struct usb_device *udev = btdata->udev; | |
2660 | size_t count, size, sent = 0; | |
2661 | int pipe, len, err; | |
2662 | u8 *buf; | |
2663 | ||
2664 | buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL); | |
2665 | if (!buf) | |
2666 | return -ENOMEM; | |
2667 | ||
2668 | count = firmware->size; | |
2669 | ||
2670 | size = min_t(size_t, count, hdr_size); | |
2671 | memcpy(buf, firmware->data, size); | |
2672 | ||
2673 | /* USB patches should go down to controller through USB path | |
2674 | * because binary format fits to go down through USB channel. | |
2675 | * USB control path is for patching headers and USB bulk is for | |
2676 | * patch body. | |
2677 | */ | |
2678 | pipe = usb_sndctrlpipe(udev, 0); | |
2679 | err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR, | |
2680 | 0, 0, buf, size, USB_CTRL_SET_TIMEOUT); | |
2681 | if (err < 0) { | |
2064ee33 | 2682 | bt_dev_err(hdev, "Failed to send headers (%d)", err); |
3267c884 KBYT |
2683 | goto done; |
2684 | } | |
2685 | ||
2686 | sent += size; | |
2687 | count -= size; | |
2688 | ||
2689 | while (count) { | |
2690 | size = min_t(size_t, count, QCA_DFU_PACKET_LEN); | |
2691 | ||
2692 | memcpy(buf, firmware->data + sent, size); | |
2693 | ||
2694 | pipe = usb_sndbulkpipe(udev, 0x02); | |
2695 | err = usb_bulk_msg(udev, pipe, buf, size, &len, | |
2696 | QCA_DFU_TIMEOUT); | |
2697 | if (err < 0) { | |
2064ee33 MH |
2698 | bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)", |
2699 | sent, firmware->size, err); | |
3267c884 KBYT |
2700 | break; |
2701 | } | |
2702 | ||
2703 | if (size != len) { | |
2064ee33 | 2704 | bt_dev_err(hdev, "Failed to get bulk buffer"); |
3267c884 KBYT |
2705 | err = -EILSEQ; |
2706 | break; | |
2707 | } | |
2708 | ||
2709 | sent += size; | |
2710 | count -= size; | |
2711 | } | |
2712 | ||
2713 | done: | |
2714 | kfree(buf); | |
2715 | return err; | |
2716 | } | |
2717 | ||
2718 | static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev, | |
2719 | struct qca_version *ver, | |
2720 | const struct qca_device_info *info) | |
2721 | { | |
2722 | struct qca_rampatch_version *rver; | |
2723 | const struct firmware *fw; | |
bf906b3d KBYT |
2724 | u32 ver_rom, ver_patch; |
2725 | u16 rver_rom, rver_patch; | |
3267c884 KBYT |
2726 | char fwname[64]; |
2727 | int err; | |
2728 | ||
bf906b3d KBYT |
2729 | ver_rom = le32_to_cpu(ver->rom_version); |
2730 | ver_patch = le32_to_cpu(ver->patch_version); | |
2731 | ||
2732 | snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom); | |
3267c884 KBYT |
2733 | |
2734 | err = request_firmware(&fw, fwname, &hdev->dev); | |
2735 | if (err) { | |
2064ee33 MH |
2736 | bt_dev_err(hdev, "failed to request rampatch file: %s (%d)", |
2737 | fwname, err); | |
3267c884 KBYT |
2738 | return err; |
2739 | } | |
2740 | ||
2064ee33 | 2741 | bt_dev_info(hdev, "using rampatch file: %s", fwname); |
bf906b3d | 2742 | |
3267c884 | 2743 | rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset); |
bf906b3d KBYT |
2744 | rver_rom = le16_to_cpu(rver->rom_version); |
2745 | rver_patch = le16_to_cpu(rver->patch_version); | |
2746 | ||
2064ee33 MH |
2747 | bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, " |
2748 | "firmware rome 0x%x build 0x%x", | |
2749 | rver_rom, rver_patch, ver_rom, ver_patch); | |
3267c884 | 2750 | |
bf906b3d | 2751 | if (rver_rom != ver_rom || rver_patch <= ver_patch) { |
2064ee33 | 2752 | bt_dev_err(hdev, "rampatch file version did not match with firmware"); |
3267c884 KBYT |
2753 | err = -EINVAL; |
2754 | goto done; | |
2755 | } | |
2756 | ||
2757 | err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr); | |
2758 | ||
2759 | done: | |
2760 | release_firmware(fw); | |
2761 | ||
2762 | return err; | |
2763 | } | |
2764 | ||
2765 | static int btusb_setup_qca_load_nvm(struct hci_dev *hdev, | |
2766 | struct qca_version *ver, | |
2767 | const struct qca_device_info *info) | |
2768 | { | |
2769 | const struct firmware *fw; | |
2770 | char fwname[64]; | |
2771 | int err; | |
2772 | ||
2773 | snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin", | |
2774 | le32_to_cpu(ver->rom_version)); | |
2775 | ||
2776 | err = request_firmware(&fw, fwname, &hdev->dev); | |
2777 | if (err) { | |
2064ee33 MH |
2778 | bt_dev_err(hdev, "failed to request NVM file: %s (%d)", |
2779 | fwname, err); | |
3267c884 KBYT |
2780 | return err; |
2781 | } | |
2782 | ||
2064ee33 | 2783 | bt_dev_info(hdev, "using NVM file: %s", fwname); |
3267c884 KBYT |
2784 | |
2785 | err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr); | |
2786 | ||
2787 | release_firmware(fw); | |
2788 | ||
2789 | return err; | |
2790 | } | |
2791 | ||
e68fc8b7 TI |
2792 | /* identify the ROM version and check whether patches are needed */ |
2793 | static bool btusb_qca_need_patch(struct usb_device *udev) | |
2794 | { | |
2795 | struct qca_version ver; | |
2796 | ||
2797 | if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver, | |
2798 | sizeof(ver)) < 0) | |
2799 | return false; | |
2800 | /* only low ROM versions need patches */ | |
2801 | return !(le32_to_cpu(ver.rom_version) & ~0xffffU); | |
2802 | } | |
2803 | ||
3267c884 KBYT |
2804 | static int btusb_setup_qca(struct hci_dev *hdev) |
2805 | { | |
e68fc8b7 TI |
2806 | struct btusb_data *btdata = hci_get_drvdata(hdev); |
2807 | struct usb_device *udev = btdata->udev; | |
3267c884 KBYT |
2808 | const struct qca_device_info *info = NULL; |
2809 | struct qca_version ver; | |
bf906b3d | 2810 | u32 ver_rom; |
3267c884 KBYT |
2811 | u8 status; |
2812 | int i, err; | |
2813 | ||
e68fc8b7 | 2814 | err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver, |
eb50042f | 2815 | sizeof(ver)); |
3267c884 KBYT |
2816 | if (err < 0) |
2817 | return err; | |
2818 | ||
bf906b3d | 2819 | ver_rom = le32_to_cpu(ver.rom_version); |
e68fc8b7 TI |
2820 | /* Don't care about high ROM versions */ |
2821 | if (ver_rom & ~0xffffU) | |
2822 | return 0; | |
2823 | ||
3267c884 | 2824 | for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) { |
bf906b3d | 2825 | if (ver_rom == qca_devices_table[i].rom_version) |
3267c884 KBYT |
2826 | info = &qca_devices_table[i]; |
2827 | } | |
2828 | if (!info) { | |
2064ee33 | 2829 | bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom); |
3267c884 KBYT |
2830 | return -ENODEV; |
2831 | } | |
2832 | ||
e68fc8b7 | 2833 | err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status, |
3267c884 KBYT |
2834 | sizeof(status)); |
2835 | if (err < 0) | |
2836 | return err; | |
2837 | ||
2838 | if (!(status & QCA_PATCH_UPDATED)) { | |
2839 | err = btusb_setup_qca_load_rampatch(hdev, &ver, info); | |
2840 | if (err < 0) | |
2841 | return err; | |
2842 | } | |
2843 | ||
2844 | if (!(status & QCA_SYSCFG_UPDATED)) { | |
2845 | err = btusb_setup_qca_load_nvm(hdev, &ver, info); | |
2846 | if (err < 0) | |
2847 | return err; | |
2848 | } | |
2849 | ||
2850 | return 0; | |
2851 | } | |
2852 | ||
9d08f504 MH |
2853 | #ifdef CONFIG_BT_HCIBTUSB_BCM |
2854 | static inline int __set_diag_interface(struct hci_dev *hdev) | |
2855 | { | |
2856 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2857 | struct usb_interface *intf = data->diag; | |
2858 | int i; | |
2859 | ||
2860 | if (!data->diag) | |
2861 | return -ENODEV; | |
2862 | ||
2863 | data->diag_tx_ep = NULL; | |
2864 | data->diag_rx_ep = NULL; | |
2865 | ||
2866 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | |
2867 | struct usb_endpoint_descriptor *ep_desc; | |
2868 | ||
2869 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; | |
2870 | ||
2871 | if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) { | |
2872 | data->diag_tx_ep = ep_desc; | |
2873 | continue; | |
2874 | } | |
2875 | ||
2876 | if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) { | |
2877 | data->diag_rx_ep = ep_desc; | |
2878 | continue; | |
2879 | } | |
2880 | } | |
2881 | ||
2882 | if (!data->diag_tx_ep || !data->diag_rx_ep) { | |
2064ee33 | 2883 | bt_dev_err(hdev, "invalid diagnostic descriptors"); |
9d08f504 MH |
2884 | return -ENODEV; |
2885 | } | |
2886 | ||
2887 | return 0; | |
2888 | } | |
2889 | ||
2890 | static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable) | |
2891 | { | |
2892 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2893 | struct sk_buff *skb; | |
2894 | struct urb *urb; | |
2895 | unsigned int pipe; | |
2896 | ||
2897 | if (!data->diag_tx_ep) | |
2898 | return ERR_PTR(-ENODEV); | |
2899 | ||
2900 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
2901 | if (!urb) | |
2902 | return ERR_PTR(-ENOMEM); | |
2903 | ||
2904 | skb = bt_skb_alloc(2, GFP_KERNEL); | |
2905 | if (!skb) { | |
2906 | usb_free_urb(urb); | |
2907 | return ERR_PTR(-ENOMEM); | |
2908 | } | |
2909 | ||
634fef61 JB |
2910 | skb_put_u8(skb, 0xf0); |
2911 | skb_put_u8(skb, enable); | |
9d08f504 MH |
2912 | |
2913 | pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress); | |
2914 | ||
2915 | usb_fill_bulk_urb(urb, data->udev, pipe, | |
2916 | skb->data, skb->len, btusb_tx_complete, skb); | |
2917 | ||
2918 | skb->dev = (void *)hdev; | |
2919 | ||
2920 | return urb; | |
2921 | } | |
2922 | ||
2923 | static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) | |
2924 | { | |
2925 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2926 | struct urb *urb; | |
2927 | ||
2928 | if (!data->diag) | |
2929 | return -ENODEV; | |
2930 | ||
2931 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
2932 | return -ENETDOWN; | |
2933 | ||
2934 | urb = alloc_diag_urb(hdev, enable); | |
2935 | if (IS_ERR(urb)) | |
2936 | return PTR_ERR(urb); | |
2937 | ||
2938 | return submit_or_queue_tx_urb(hdev, urb); | |
2939 | } | |
2940 | #endif | |
2941 | ||
fd913ef7 RJ |
2942 | #ifdef CONFIG_PM |
2943 | static irqreturn_t btusb_oob_wake_handler(int irq, void *priv) | |
2944 | { | |
2945 | struct btusb_data *data = priv; | |
2946 | ||
2947 | pm_wakeup_event(&data->udev->dev, 0); | |
017789f3 | 2948 | pm_system_wakeup(); |
fd913ef7 RJ |
2949 | |
2950 | /* Disable only if not already disabled (keep it balanced) */ | |
2951 | if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) { | |
2952 | disable_irq_nosync(irq); | |
2953 | disable_irq_wake(irq); | |
2954 | } | |
2955 | return IRQ_HANDLED; | |
2956 | } | |
2957 | ||
2958 | static const struct of_device_id btusb_match_table[] = { | |
2959 | { .compatible = "usb1286,204e" }, | |
2960 | { } | |
2961 | }; | |
2962 | MODULE_DEVICE_TABLE(of, btusb_match_table); | |
2963 | ||
2964 | /* Use an oob wakeup pin? */ | |
2965 | static int btusb_config_oob_wake(struct hci_dev *hdev) | |
2966 | { | |
2967 | struct btusb_data *data = hci_get_drvdata(hdev); | |
2968 | struct device *dev = &data->udev->dev; | |
2969 | int irq, ret; | |
2970 | ||
2971 | clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags); | |
2972 | ||
2973 | if (!of_match_device(btusb_match_table, dev)) | |
2974 | return 0; | |
2975 | ||
2976 | /* Move on if no IRQ specified */ | |
2977 | irq = of_irq_get_byname(dev->of_node, "wakeup"); | |
2978 | if (irq <= 0) { | |
2979 | bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__); | |
2980 | return 0; | |
2981 | } | |
2982 | ||
2983 | ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler, | |
2984 | 0, "OOB Wake-on-BT", data); | |
2985 | if (ret) { | |
2986 | bt_dev_err(hdev, "%s: IRQ request failed", __func__); | |
2987 | return ret; | |
2988 | } | |
2989 | ||
2990 | ret = device_init_wakeup(dev, true); | |
2991 | if (ret) { | |
2992 | bt_dev_err(hdev, "%s: failed to init_wakeup", __func__); | |
2993 | return ret; | |
2994 | } | |
2995 | ||
2996 | data->oob_wake_irq = irq; | |
2997 | disable_irq(irq); | |
2998 | bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq); | |
2999 | return 0; | |
3000 | } | |
3001 | #endif | |
3002 | ||
2eaeba6d HG |
3003 | static void btusb_check_needs_reset_resume(struct usb_interface *intf) |
3004 | { | |
3005 | if (dmi_check_system(btusb_needs_reset_resume_table)) | |
3006 | interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME; | |
3007 | } | |
3008 | ||
5e23b923 | 3009 | static int btusb_probe(struct usb_interface *intf, |
89e7533d | 3010 | const struct usb_device_id *id) |
5e23b923 MH |
3011 | { |
3012 | struct usb_endpoint_descriptor *ep_desc; | |
3013 | struct btusb_data *data; | |
3014 | struct hci_dev *hdev; | |
22f8e9db | 3015 | unsigned ifnum_base; |
5e23b923 MH |
3016 | int i, err; |
3017 | ||
3018 | BT_DBG("intf %p id %p", intf, id); | |
3019 | ||
cfeb4145 | 3020 | /* interface numbers are hardcoded in the spec */ |
22f8e9db MH |
3021 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) { |
3022 | if (!(id->driver_info & BTUSB_IFNUM_2)) | |
3023 | return -ENODEV; | |
3024 | if (intf->cur_altsetting->desc.bInterfaceNumber != 2) | |
3025 | return -ENODEV; | |
3026 | } | |
3027 | ||
3028 | ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber; | |
5e23b923 MH |
3029 | |
3030 | if (!id->driver_info) { | |
3031 | const struct usb_device_id *match; | |
89e7533d | 3032 | |
5e23b923 MH |
3033 | match = usb_match_id(intf, blacklist_table); |
3034 | if (match) | |
3035 | id = match; | |
3036 | } | |
3037 | ||
cfeb4145 MH |
3038 | if (id->driver_info == BTUSB_IGNORE) |
3039 | return -ENODEV; | |
3040 | ||
2d25f8b4 SL |
3041 | if (id->driver_info & BTUSB_ATH3012) { |
3042 | struct usb_device *udev = interface_to_usbdev(intf); | |
3043 | ||
3044 | /* Old firmware would otherwise let ath3k driver load | |
d98422cb DR |
3045 | * patch and sysconfig files |
3046 | */ | |
e68fc8b7 TI |
3047 | if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 && |
3048 | !btusb_qca_need_patch(udev)) | |
2d25f8b4 SL |
3049 | return -ENODEV; |
3050 | } | |
3051 | ||
98921dbd | 3052 | data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL); |
5e23b923 MH |
3053 | if (!data) |
3054 | return -ENOMEM; | |
3055 | ||
3056 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | |
3057 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; | |
3058 | ||
3059 | if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) { | |
3060 | data->intr_ep = ep_desc; | |
3061 | continue; | |
3062 | } | |
3063 | ||
3064 | if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) { | |
3065 | data->bulk_tx_ep = ep_desc; | |
3066 | continue; | |
3067 | } | |
3068 | ||
3069 | if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) { | |
3070 | data->bulk_rx_ep = ep_desc; | |
3071 | continue; | |
3072 | } | |
3073 | } | |
3074 | ||
98921dbd | 3075 | if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) |
5e23b923 | 3076 | return -ENODEV; |
5e23b923 | 3077 | |
893ba544 MH |
3078 | if (id->driver_info & BTUSB_AMP) { |
3079 | data->cmdreq_type = USB_TYPE_CLASS | 0x01; | |
3080 | data->cmdreq = 0x2b; | |
3081 | } else { | |
3082 | data->cmdreq_type = USB_TYPE_CLASS; | |
3083 | data->cmdreq = 0x00; | |
3084 | } | |
7a9d4020 | 3085 | |
5e23b923 | 3086 | data->udev = interface_to_usbdev(intf); |
5fbcd260 | 3087 | data->intf = intf; |
5e23b923 | 3088 | |
5e23b923 | 3089 | INIT_WORK(&data->work, btusb_work); |
7bee549e | 3090 | INIT_WORK(&data->waker, btusb_waker); |
803b5836 MH |
3091 | init_usb_anchor(&data->deferred); |
3092 | init_usb_anchor(&data->tx_anchor); | |
7bee549e | 3093 | spin_lock_init(&data->txlock); |
5e23b923 | 3094 | |
5e23b923 MH |
3095 | init_usb_anchor(&data->intr_anchor); |
3096 | init_usb_anchor(&data->bulk_anchor); | |
9bfa35fe | 3097 | init_usb_anchor(&data->isoc_anchor); |
9d08f504 | 3098 | init_usb_anchor(&data->diag_anchor); |
803b5836 | 3099 | spin_lock_init(&data->rxlock); |
5e23b923 | 3100 | |
cda0dd78 MH |
3101 | if (id->driver_info & BTUSB_INTEL_NEW) { |
3102 | data->recv_event = btusb_recv_event_intel; | |
3103 | data->recv_bulk = btusb_recv_bulk_intel; | |
3104 | set_bit(BTUSB_BOOTLOADER, &data->flags); | |
3105 | } else { | |
3106 | data->recv_event = hci_recv_frame; | |
3107 | data->recv_bulk = btusb_recv_bulk; | |
3108 | } | |
2cbd3f5c | 3109 | |
5e23b923 | 3110 | hdev = hci_alloc_dev(); |
98921dbd | 3111 | if (!hdev) |
5e23b923 | 3112 | return -ENOMEM; |
5e23b923 | 3113 | |
c13854ce | 3114 | hdev->bus = HCI_USB; |
155961e8 | 3115 | hci_set_drvdata(hdev, data); |
5e23b923 | 3116 | |
893ba544 MH |
3117 | if (id->driver_info & BTUSB_AMP) |
3118 | hdev->dev_type = HCI_AMP; | |
3119 | else | |
ca8bee5d | 3120 | hdev->dev_type = HCI_PRIMARY; |
893ba544 | 3121 | |
5e23b923 MH |
3122 | data->hdev = hdev; |
3123 | ||
3124 | SET_HCIDEV_DEV(hdev, &intf->dev); | |
3125 | ||
9f8f962c MH |
3126 | hdev->open = btusb_open; |
3127 | hdev->close = btusb_close; | |
3128 | hdev->flush = btusb_flush; | |
3129 | hdev->send = btusb_send_frame; | |
3130 | hdev->notify = btusb_notify; | |
3131 | ||
fd913ef7 RJ |
3132 | #ifdef CONFIG_PM |
3133 | err = btusb_config_oob_wake(hdev); | |
3134 | if (err) | |
3135 | goto out_free_dev; | |
a4ccc9e3 RJ |
3136 | |
3137 | /* Marvell devices may need a specific chip configuration */ | |
3138 | if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) { | |
3139 | err = marvell_config_oob_wake(hdev); | |
3140 | if (err) | |
3141 | goto out_free_dev; | |
3142 | } | |
fd913ef7 | 3143 | #endif |
418678b0 SJ |
3144 | if (id->driver_info & BTUSB_CW6622) |
3145 | set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks); | |
3146 | ||
6c9d435d MH |
3147 | if (id->driver_info & BTUSB_BCM2045) |
3148 | set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks); | |
3149 | ||
9f8f962c MH |
3150 | if (id->driver_info & BTUSB_BCM92035) |
3151 | hdev->setup = btusb_setup_bcm92035; | |
5e23b923 | 3152 | |
c2bfb100 | 3153 | #ifdef CONFIG_BT_HCIBTUSB_BCM |
abbaf50e | 3154 | if (id->driver_info & BTUSB_BCM_PATCHRAM) { |
49a5f782 | 3155 | hdev->manufacturer = 15; |
c2bfb100 | 3156 | hdev->setup = btbcm_setup_patchram; |
9d08f504 | 3157 | hdev->set_diag = btusb_bcm_set_diag; |
1df1f591 | 3158 | hdev->set_bdaddr = btbcm_set_bdaddr; |
9d08f504 MH |
3159 | |
3160 | /* Broadcom LM_DIAG Interface numbers are hardcoded */ | |
22f8e9db | 3161 | data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2); |
abbaf50e | 3162 | } |
10d4c673 | 3163 | |
9d08f504 | 3164 | if (id->driver_info & BTUSB_BCM_APPLE) { |
49a5f782 | 3165 | hdev->manufacturer = 15; |
c2bfb100 | 3166 | hdev->setup = btbcm_setup_apple; |
9d08f504 MH |
3167 | hdev->set_diag = btusb_bcm_set_diag; |
3168 | ||
3169 | /* Broadcom LM_DIAG Interface numbers are hardcoded */ | |
22f8e9db | 3170 | data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2); |
9d08f504 | 3171 | } |
c2bfb100 | 3172 | #endif |
17b2772b | 3173 | |
cb8d6597 | 3174 | if (id->driver_info & BTUSB_INTEL) { |
49a5f782 | 3175 | hdev->manufacturer = 2; |
dffd30ee | 3176 | hdev->setup = btusb_setup_intel; |
bfbd45e9 | 3177 | hdev->shutdown = btusb_shutdown_intel; |
3e24767b | 3178 | hdev->set_diag = btintel_set_diag_mfg; |
4185a0f5 | 3179 | hdev->set_bdaddr = btintel_set_bdaddr; |
c33fb9b4 | 3180 | set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); |
c1154842 | 3181 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
3e24767b | 3182 | set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks); |
cb8d6597 | 3183 | } |
dffd30ee | 3184 | |
cda0dd78 | 3185 | if (id->driver_info & BTUSB_INTEL_NEW) { |
49a5f782 | 3186 | hdev->manufacturer = 2; |
cda0dd78 MH |
3187 | hdev->send = btusb_send_frame_intel; |
3188 | hdev->setup = btusb_setup_intel_new; | |
eeb6abe9 | 3189 | hdev->hw_error = btintel_hw_error; |
6d2e50d2 | 3190 | hdev->set_diag = btintel_set_diag; |
4185a0f5 | 3191 | hdev->set_bdaddr = btintel_set_bdaddr; |
b970c5ba | 3192 | set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); |
d8270fbb | 3193 | set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks); |
cda0dd78 MH |
3194 | } |
3195 | ||
84845ceb WJS |
3196 | if (id->driver_info & BTUSB_MARVELL) { |
3197 | struct pci_dev *pdev; | |
ae8df494 | 3198 | hdev->set_bdaddr = btusb_set_bdaddr_marvell; |
84845ceb WJS |
3199 | pdev = pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, 0x1028, 0x0720, NULL); |
3200 | if (!pdev) | |
3201 | pdev = pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, 0x1028, 0x0733, NULL); | |
3202 | if (pdev) { | |
3203 | pci_dev_put(pdev); | |
3204 | hdev->post_init = btusb_edge_post_init; | |
3205 | hdev->shutdown = btusb_edge_shutdown; | |
3206 | } | |
3207 | } | |
ae8df494 | 3208 | |
661cf88a MH |
3209 | if (id->driver_info & BTUSB_SWAVE) { |
3210 | set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks); | |
d57dbe77 | 3211 | set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks); |
661cf88a | 3212 | } |
d57dbe77 | 3213 | |
e4c534bb MH |
3214 | if (id->driver_info & BTUSB_INTEL_BOOT) { |
3215 | hdev->manufacturer = 2; | |
40df783d | 3216 | set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); |
e4c534bb | 3217 | } |
40df783d | 3218 | |
79f0c87d | 3219 | if (id->driver_info & BTUSB_ATH3012) { |
e68fc8b7 | 3220 | data->setup_on_usb = btusb_setup_qca; |
5859223e | 3221 | hdev->set_bdaddr = btusb_set_bdaddr_ath3012; |
3d50d51a | 3222 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
79f0c87d JP |
3223 | set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); |
3224 | } | |
5859223e | 3225 | |
3267c884 KBYT |
3226 | if (id->driver_info & BTUSB_QCA_ROME) { |
3227 | data->setup_on_usb = btusb_setup_qca; | |
3228 | hdev->set_bdaddr = btusb_set_bdaddr_ath3012; | |
2eaeba6d | 3229 | btusb_check_needs_reset_resume(intf); |
3267c884 KBYT |
3230 | } |
3231 | ||
db33c77d | 3232 | #ifdef CONFIG_BT_HCIBTUSB_RTL |
04b8c814 | 3233 | if (id->driver_info & BTUSB_REALTEK) { |
db33c77d | 3234 | hdev->setup = btrtl_setup_realtek; |
04b8c814 DD |
3235 | |
3236 | /* Realtek devices lose their updated firmware over suspend, | |
3237 | * but the USB hub doesn't notice any status change. | |
3238 | * Explicitly request a device reset on resume. | |
3239 | */ | |
18970430 | 3240 | interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME; |
04b8c814 | 3241 | } |
db33c77d | 3242 | #endif |
a2698a9b | 3243 | |
893ba544 MH |
3244 | if (id->driver_info & BTUSB_AMP) { |
3245 | /* AMP controllers do not support SCO packets */ | |
3246 | data->isoc = NULL; | |
3247 | } else { | |
22f8e9db MH |
3248 | /* Interface orders are hardcoded in the specification */ |
3249 | data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1); | |
459232fc | 3250 | data->isoc_ifnum = ifnum_base + 1; |
893ba544 | 3251 | } |
9bfa35fe | 3252 | |
7a9d4020 | 3253 | if (!reset) |
a6c511c6 | 3254 | set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); |
cfeb4145 MH |
3255 | |
3256 | if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) { | |
3257 | if (!disable_scofix) | |
3258 | set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks); | |
3259 | } | |
3260 | ||
9bfa35fe MH |
3261 | if (id->driver_info & BTUSB_BROKEN_ISOC) |
3262 | data->isoc = NULL; | |
3263 | ||
7a9d4020 MH |
3264 | if (id->driver_info & BTUSB_DIGIANSWER) { |
3265 | data->cmdreq_type = USB_TYPE_VENDOR; | |
a6c511c6 | 3266 | set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); |
7a9d4020 MH |
3267 | } |
3268 | ||
3269 | if (id->driver_info & BTUSB_CSR) { | |
3270 | struct usb_device *udev = data->udev; | |
81cac64b | 3271 | u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); |
7a9d4020 MH |
3272 | |
3273 | /* Old firmware would otherwise execute USB reset */ | |
81cac64b | 3274 | if (bcdDevice < 0x117) |
a6c511c6 | 3275 | set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); |
81cac64b MH |
3276 | |
3277 | /* Fake CSR devices with broken commands */ | |
6cafcd95 | 3278 | if (bcdDevice <= 0x100 || bcdDevice == 0x134) |
81cac64b | 3279 | hdev->setup = btusb_setup_csr; |
49c989a0 JP |
3280 | |
3281 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); | |
7a9d4020 MH |
3282 | } |
3283 | ||
cfeb4145 | 3284 | if (id->driver_info & BTUSB_SNIFFER) { |
9bfa35fe | 3285 | struct usb_device *udev = data->udev; |
cfeb4145 | 3286 | |
7a9d4020 | 3287 | /* New sniffer firmware has crippled HCI interface */ |
cfeb4145 MH |
3288 | if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997) |
3289 | set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); | |
3290 | } | |
3291 | ||
3a5ef20c MH |
3292 | if (id->driver_info & BTUSB_INTEL_BOOT) { |
3293 | /* A bug in the bootloader causes that interrupt interface is | |
3294 | * only enabled after receiving SetInterface(0, AltSetting=0). | |
3295 | */ | |
3296 | err = usb_set_interface(data->udev, 0, 0); | |
3297 | if (err < 0) { | |
3298 | BT_ERR("failed to set interface 0, alt 0 %d", err); | |
10ab133b | 3299 | goto out_free_dev; |
3a5ef20c MH |
3300 | } |
3301 | } | |
3302 | ||
9bfa35fe MH |
3303 | if (data->isoc) { |
3304 | err = usb_driver_claim_interface(&btusb_driver, | |
89e7533d | 3305 | data->isoc, data); |
10ab133b RJ |
3306 | if (err < 0) |
3307 | goto out_free_dev; | |
9bfa35fe MH |
3308 | } |
3309 | ||
9d08f504 MH |
3310 | #ifdef CONFIG_BT_HCIBTUSB_BCM |
3311 | if (data->diag) { | |
3312 | if (!usb_driver_claim_interface(&btusb_driver, | |
3313 | data->diag, data)) | |
3314 | __set_diag_interface(hdev); | |
3315 | else | |
3316 | data->diag = NULL; | |
3317 | } | |
3318 | #endif | |
3319 | ||
5e23b923 | 3320 | err = hci_register_dev(hdev); |
10ab133b RJ |
3321 | if (err < 0) |
3322 | goto out_free_dev; | |
5e23b923 MH |
3323 | |
3324 | usb_set_intfdata(intf, data); | |
3325 | ||
3326 | return 0; | |
10ab133b RJ |
3327 | |
3328 | out_free_dev: | |
3329 | hci_free_dev(hdev); | |
3330 | return err; | |
5e23b923 MH |
3331 | } |
3332 | ||
3333 | static void btusb_disconnect(struct usb_interface *intf) | |
3334 | { | |
3335 | struct btusb_data *data = usb_get_intfdata(intf); | |
3336 | struct hci_dev *hdev; | |
3337 | ||
3338 | BT_DBG("intf %p", intf); | |
3339 | ||
3340 | if (!data) | |
3341 | return; | |
3342 | ||
3343 | hdev = data->hdev; | |
5fbcd260 MH |
3344 | usb_set_intfdata(data->intf, NULL); |
3345 | ||
3346 | if (data->isoc) | |
3347 | usb_set_intfdata(data->isoc, NULL); | |
5e23b923 | 3348 | |
9d08f504 MH |
3349 | if (data->diag) |
3350 | usb_set_intfdata(data->diag, NULL); | |
3351 | ||
5e23b923 MH |
3352 | hci_unregister_dev(hdev); |
3353 | ||
9d08f504 MH |
3354 | if (intf == data->intf) { |
3355 | if (data->isoc) | |
3356 | usb_driver_release_interface(&btusb_driver, data->isoc); | |
3357 | if (data->diag) | |
3358 | usb_driver_release_interface(&btusb_driver, data->diag); | |
3359 | } else if (intf == data->isoc) { | |
3360 | if (data->diag) | |
3361 | usb_driver_release_interface(&btusb_driver, data->diag); | |
5fbcd260 | 3362 | usb_driver_release_interface(&btusb_driver, data->intf); |
9d08f504 MH |
3363 | } else if (intf == data->diag) { |
3364 | usb_driver_release_interface(&btusb_driver, data->intf); | |
3365 | if (data->isoc) | |
3366 | usb_driver_release_interface(&btusb_driver, data->isoc); | |
3367 | } | |
5fbcd260 | 3368 | |
fd913ef7 RJ |
3369 | if (data->oob_wake_irq) |
3370 | device_init_wakeup(&data->udev->dev, false); | |
3371 | ||
5e23b923 MH |
3372 | hci_free_dev(hdev); |
3373 | } | |
3374 | ||
7bee549e | 3375 | #ifdef CONFIG_PM |
6a88adf2 MH |
3376 | static int btusb_suspend(struct usb_interface *intf, pm_message_t message) |
3377 | { | |
3378 | struct btusb_data *data = usb_get_intfdata(intf); | |
3379 | ||
3380 | BT_DBG("intf %p", intf); | |
3381 | ||
3382 | if (data->suspend_count++) | |
3383 | return 0; | |
3384 | ||
7bee549e | 3385 | spin_lock_irq(&data->txlock); |
5b1b0b81 | 3386 | if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) { |
7bee549e ON |
3387 | set_bit(BTUSB_SUSPENDING, &data->flags); |
3388 | spin_unlock_irq(&data->txlock); | |
3389 | } else { | |
3390 | spin_unlock_irq(&data->txlock); | |
3391 | data->suspend_count--; | |
3392 | return -EBUSY; | |
3393 | } | |
3394 | ||
6a88adf2 MH |
3395 | cancel_work_sync(&data->work); |
3396 | ||
7bee549e | 3397 | btusb_stop_traffic(data); |
6a88adf2 MH |
3398 | usb_kill_anchored_urbs(&data->tx_anchor); |
3399 | ||
fd913ef7 RJ |
3400 | if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) { |
3401 | set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags); | |
3402 | enable_irq_wake(data->oob_wake_irq); | |
3403 | enable_irq(data->oob_wake_irq); | |
3404 | } | |
3405 | ||
6a88adf2 MH |
3406 | return 0; |
3407 | } | |
3408 | ||
7bee549e ON |
3409 | static void play_deferred(struct btusb_data *data) |
3410 | { | |
3411 | struct urb *urb; | |
3412 | int err; | |
3413 | ||
3414 | while ((urb = usb_get_from_anchor(&data->deferred))) { | |
19cfe912 JC |
3415 | usb_anchor_urb(urb, &data->tx_anchor); |
3416 | ||
7bee549e | 3417 | err = usb_submit_urb(urb, GFP_ATOMIC); |
19cfe912 JC |
3418 | if (err < 0) { |
3419 | if (err != -EPERM && err != -ENODEV) | |
3420 | BT_ERR("%s urb %p submission failed (%d)", | |
3421 | data->hdev->name, urb, -err); | |
3422 | kfree(urb->setup_packet); | |
3423 | usb_unanchor_urb(urb); | |
3424 | usb_free_urb(urb); | |
7bee549e | 3425 | break; |
19cfe912 | 3426 | } |
7bee549e ON |
3427 | |
3428 | data->tx_in_flight++; | |
19cfe912 JC |
3429 | usb_free_urb(urb); |
3430 | } | |
3431 | ||
3432 | /* Cleanup the rest deferred urbs. */ | |
3433 | while ((urb = usb_get_from_anchor(&data->deferred))) { | |
3434 | kfree(urb->setup_packet); | |
3435 | usb_free_urb(urb); | |
7bee549e | 3436 | } |
7bee549e ON |
3437 | } |
3438 | ||
6a88adf2 MH |
3439 | static int btusb_resume(struct usb_interface *intf) |
3440 | { | |
3441 | struct btusb_data *data = usb_get_intfdata(intf); | |
3442 | struct hci_dev *hdev = data->hdev; | |
7bee549e | 3443 | int err = 0; |
6a88adf2 MH |
3444 | |
3445 | BT_DBG("intf %p", intf); | |
3446 | ||
3447 | if (--data->suspend_count) | |
3448 | return 0; | |
3449 | ||
fd913ef7 RJ |
3450 | /* Disable only if not already disabled (keep it balanced) */ |
3451 | if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) { | |
3452 | disable_irq(data->oob_wake_irq); | |
3453 | disable_irq_wake(data->oob_wake_irq); | |
3454 | } | |
3455 | ||
6a88adf2 | 3456 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
7bee549e | 3457 | goto done; |
6a88adf2 MH |
3458 | |
3459 | if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { | |
3460 | err = btusb_submit_intr_urb(hdev, GFP_NOIO); | |
3461 | if (err < 0) { | |
3462 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); | |
7bee549e | 3463 | goto failed; |
6a88adf2 MH |
3464 | } |
3465 | } | |
3466 | ||
3467 | if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) { | |
43c2e57f MH |
3468 | err = btusb_submit_bulk_urb(hdev, GFP_NOIO); |
3469 | if (err < 0) { | |
6a88adf2 | 3470 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); |
7bee549e ON |
3471 | goto failed; |
3472 | } | |
3473 | ||
3474 | btusb_submit_bulk_urb(hdev, GFP_NOIO); | |
6a88adf2 MH |
3475 | } |
3476 | ||
3477 | if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { | |
3478 | if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0) | |
3479 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3480 | else | |
3481 | btusb_submit_isoc_urb(hdev, GFP_NOIO); | |
3482 | } | |
3483 | ||
7bee549e ON |
3484 | spin_lock_irq(&data->txlock); |
3485 | play_deferred(data); | |
3486 | clear_bit(BTUSB_SUSPENDING, &data->flags); | |
3487 | spin_unlock_irq(&data->txlock); | |
3488 | schedule_work(&data->work); | |
3489 | ||
6a88adf2 | 3490 | return 0; |
7bee549e ON |
3491 | |
3492 | failed: | |
3493 | usb_scuttle_anchored_urbs(&data->deferred); | |
3494 | done: | |
3495 | spin_lock_irq(&data->txlock); | |
3496 | clear_bit(BTUSB_SUSPENDING, &data->flags); | |
3497 | spin_unlock_irq(&data->txlock); | |
3498 | ||
3499 | return err; | |
6a88adf2 | 3500 | } |
7bee549e | 3501 | #endif |
6a88adf2 | 3502 | |
5e23b923 MH |
3503 | static struct usb_driver btusb_driver = { |
3504 | .name = "btusb", | |
3505 | .probe = btusb_probe, | |
3506 | .disconnect = btusb_disconnect, | |
7bee549e | 3507 | #ifdef CONFIG_PM |
6a88adf2 MH |
3508 | .suspend = btusb_suspend, |
3509 | .resume = btusb_resume, | |
7bee549e | 3510 | #endif |
5e23b923 | 3511 | .id_table = btusb_table, |
7bee549e | 3512 | .supports_autosuspend = 1, |
e1f12eb6 | 3513 | .disable_hub_initiated_lpm = 1, |
5e23b923 MH |
3514 | }; |
3515 | ||
93f1508c | 3516 | module_usb_driver(btusb_driver); |
5e23b923 | 3517 | |
cfeb4145 MH |
3518 | module_param(disable_scofix, bool, 0644); |
3519 | MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size"); | |
3520 | ||
3521 | module_param(force_scofix, bool, 0644); | |
3522 | MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size"); | |
3523 | ||
3524 | module_param(reset, bool, 0644); | |
3525 | MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); | |
3526 | ||
5e23b923 MH |
3527 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
3528 | MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION); | |
3529 | MODULE_VERSION(VERSION); | |
3530 | MODULE_LICENSE("GPL"); |