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