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