Advertisement
JeremyB31

Untitled

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