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