btbcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. *
  3. * Bluetooth support for Broadcom devices
  4. *
  5. * Copyright (C) 2015 Intel Corporation
  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. #include <linux/module.h>
  24. #include <linux/firmware.h>
  25. #include <asm/unaligned.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "btbcm.h"
  29. #define VERSION "0.1"
  30. #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
  31. #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
  32. #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
  33. int btbcm_check_bdaddr(struct hci_dev *hdev)
  34. {
  35. struct hci_rp_read_bd_addr *bda;
  36. struct sk_buff *skb;
  37. skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
  38. HCI_INIT_TIMEOUT);
  39. if (IS_ERR(skb)) {
  40. int err = PTR_ERR(skb);
  41. BT_ERR("%s: BCM: Reading device address failed (%d)",
  42. hdev->name, err);
  43. return err;
  44. }
  45. if (skb->len != sizeof(*bda)) {
  46. BT_ERR("%s: BCM: Device address length mismatch", hdev->name);
  47. kfree_skb(skb);
  48. return -EIO;
  49. }
  50. bda = (struct hci_rp_read_bd_addr *)skb->data;
  51. /* Check if the address indicates a controller with either an
  52. * invalid or default address. In both cases the device needs
  53. * to be marked as not having a valid address.
  54. *
  55. * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
  56. * with no configured address.
  57. *
  58. * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
  59. * with waiting for configuration state.
  60. *
  61. * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
  62. * with waiting for configuration state.
  63. */
  64. if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
  65. !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
  66. !bacmp(&bda->bdaddr, BDADDR_BCM4330B1)) {
  67. BT_INFO("%s: BCM: Using default device address (%pMR)",
  68. hdev->name, &bda->bdaddr);
  69. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  70. }
  71. kfree_skb(skb);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
  75. int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  76. {
  77. struct sk_buff *skb;
  78. int err;
  79. skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
  80. if (IS_ERR(skb)) {
  81. err = PTR_ERR(skb);
  82. BT_ERR("%s: BCM: Change address command failed (%d)",
  83. hdev->name, err);
  84. return err;
  85. }
  86. kfree_skb(skb);
  87. return 0;
  88. }
  89. EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
  90. int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
  91. {
  92. const struct hci_command_hdr *cmd;
  93. const u8 *fw_ptr;
  94. size_t fw_size;
  95. struct sk_buff *skb;
  96. u16 opcode;
  97. int err = 0;
  98. /* Start Download */
  99. skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
  100. if (IS_ERR(skb)) {
  101. err = PTR_ERR(skb);
  102. BT_ERR("%s: BCM: Download Minidrv command failed (%d)",
  103. hdev->name, err);
  104. goto done;
  105. }
  106. kfree_skb(skb);
  107. /* 50 msec delay after Download Minidrv completes */
  108. msleep(50);
  109. fw_ptr = fw->data;
  110. fw_size = fw->size;
  111. while (fw_size >= sizeof(*cmd)) {
  112. const u8 *cmd_param;
  113. cmd = (struct hci_command_hdr *)fw_ptr;
  114. fw_ptr += sizeof(*cmd);
  115. fw_size -= sizeof(*cmd);
  116. if (fw_size < cmd->plen) {
  117. BT_ERR("%s: BCM: Patch is corrupted", hdev->name);
  118. err = -EINVAL;
  119. goto done;
  120. }
  121. cmd_param = fw_ptr;
  122. fw_ptr += cmd->plen;
  123. fw_size -= cmd->plen;
  124. opcode = le16_to_cpu(cmd->opcode);
  125. skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
  126. HCI_INIT_TIMEOUT);
  127. if (IS_ERR(skb)) {
  128. err = PTR_ERR(skb);
  129. BT_ERR("%s: BCM: Patch command %04x failed (%d)",
  130. hdev->name, opcode, err);
  131. goto done;
  132. }
  133. kfree_skb(skb);
  134. }
  135. /* 250 msec delay after Launch Ram completes */
  136. msleep(250);
  137. done:
  138. return err;
  139. }
  140. EXPORT_SYMBOL(btbcm_patchram);
  141. static int btbcm_reset(struct hci_dev *hdev)
  142. {
  143. struct sk_buff *skb;
  144. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  145. if (IS_ERR(skb)) {
  146. int err = PTR_ERR(skb);
  147. BT_ERR("%s: BCM: Reset failed (%d)", hdev->name, err);
  148. return err;
  149. }
  150. kfree_skb(skb);
  151. return 0;
  152. }
  153. static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
  154. {
  155. struct sk_buff *skb;
  156. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
  157. HCI_INIT_TIMEOUT);
  158. if (IS_ERR(skb)) {
  159. BT_ERR("%s: BCM: Reading local name failed (%ld)",
  160. hdev->name, PTR_ERR(skb));
  161. return skb;
  162. }
  163. if (skb->len != sizeof(struct hci_rp_read_local_name)) {
  164. BT_ERR("%s: BCM: Local name length mismatch", hdev->name);
  165. kfree_skb(skb);
  166. return ERR_PTR(-EIO);
  167. }
  168. return skb;
  169. }
  170. static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
  171. {
  172. struct sk_buff *skb;
  173. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  174. HCI_INIT_TIMEOUT);
  175. if (IS_ERR(skb)) {
  176. BT_ERR("%s: BCM: Reading local version info failed (%ld)",
  177. hdev->name, PTR_ERR(skb));
  178. return skb;
  179. }
  180. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  181. BT_ERR("%s: BCM: Local version length mismatch", hdev->name);
  182. kfree_skb(skb);
  183. return ERR_PTR(-EIO);
  184. }
  185. return skb;
  186. }
  187. static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
  188. {
  189. struct sk_buff *skb;
  190. skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
  191. if (IS_ERR(skb)) {
  192. BT_ERR("%s: BCM: Read verbose config info failed (%ld)",
  193. hdev->name, PTR_ERR(skb));
  194. return skb;
  195. }
  196. if (skb->len != 7) {
  197. BT_ERR("%s: BCM: Verbose config length mismatch", hdev->name);
  198. kfree_skb(skb);
  199. return ERR_PTR(-EIO);
  200. }
  201. return skb;
  202. }
  203. static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
  204. {
  205. struct sk_buff *skb;
  206. skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
  207. if (IS_ERR(skb)) {
  208. BT_ERR("%s: BCM: Read USB product info failed (%ld)",
  209. hdev->name, PTR_ERR(skb));
  210. return skb;
  211. }
  212. if (skb->len != 5) {
  213. BT_ERR("%s: BCM: USB product length mismatch", hdev->name);
  214. kfree_skb(skb);
  215. return ERR_PTR(-EIO);
  216. }
  217. return skb;
  218. }
  219. static const struct {
  220. u16 subver;
  221. const char *name;
  222. } bcm_uart_subver_table[] = {
  223. { 0x4103, "BCM4330B1" }, /* 002.001.003 */
  224. { 0x410e, "BCM43341B0" }, /* 002.001.014 */
  225. { 0x4406, "BCM4324B3" }, /* 002.004.006 */
  226. { 0x6109, "BCM4335C0" }, /* 003.001.009 */
  227. { 0x610c, "BCM4354" }, /* 003.001.012 */
  228. { }
  229. };
  230. int btbcm_initialize(struct hci_dev *hdev, char *fw_name, size_t len)
  231. {
  232. u16 subver, rev;
  233. const char *hw_name = NULL;
  234. struct sk_buff *skb;
  235. struct hci_rp_read_local_version *ver;
  236. int i, err;
  237. /* Reset */
  238. err = btbcm_reset(hdev);
  239. if (err)
  240. return err;
  241. /* Read Local Version Info */
  242. skb = btbcm_read_local_version(hdev);
  243. if (IS_ERR(skb))
  244. return PTR_ERR(skb);
  245. ver = (struct hci_rp_read_local_version *)skb->data;
  246. rev = le16_to_cpu(ver->hci_rev);
  247. subver = le16_to_cpu(ver->lmp_subver);
  248. kfree_skb(skb);
  249. /* Read Verbose Config Version Info */
  250. skb = btbcm_read_verbose_config(hdev);
  251. if (IS_ERR(skb))
  252. return PTR_ERR(skb);
  253. BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
  254. kfree_skb(skb);
  255. switch ((rev & 0xf000) >> 12) {
  256. case 0:
  257. case 1:
  258. case 3:
  259. for (i = 0; bcm_uart_subver_table[i].name; i++) {
  260. if (subver == bcm_uart_subver_table[i].subver) {
  261. hw_name = bcm_uart_subver_table[i].name;
  262. break;
  263. }
  264. }
  265. snprintf(fw_name, len, "brcm/%s.hcd", hw_name ? : "BCM");
  266. break;
  267. default:
  268. return 0;
  269. }
  270. BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
  271. hw_name ? : "BCM", (subver & 0xe000) >> 13,
  272. (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL_GPL(btbcm_initialize);
  276. int btbcm_finalize(struct hci_dev *hdev)
  277. {
  278. struct sk_buff *skb;
  279. struct hci_rp_read_local_version *ver;
  280. u16 subver, rev;
  281. int err;
  282. /* Reset */
  283. err = btbcm_reset(hdev);
  284. if (err)
  285. return err;
  286. /* Read Local Version Info */
  287. skb = btbcm_read_local_version(hdev);
  288. if (IS_ERR(skb))
  289. return PTR_ERR(skb);
  290. ver = (struct hci_rp_read_local_version *)skb->data;
  291. rev = le16_to_cpu(ver->hci_rev);
  292. subver = le16_to_cpu(ver->lmp_subver);
  293. kfree_skb(skb);
  294. BT_INFO("%s: BCM (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
  295. (subver & 0xe000) >> 13, (subver & 0x1f00) >> 8,
  296. (subver & 0x00ff), rev & 0x0fff);
  297. btbcm_check_bdaddr(hdev);
  298. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL_GPL(btbcm_finalize);
  302. static const struct {
  303. u16 subver;
  304. const char *name;
  305. } bcm_usb_subver_table[] = {
  306. { 0x210b, "BCM43142A0" }, /* 001.001.011 */
  307. { 0x2112, "BCM4314A0" }, /* 001.001.018 */
  308. { 0x2118, "BCM20702A0" }, /* 001.001.024 */
  309. { 0x2126, "BCM4335A0" }, /* 001.001.038 */
  310. { 0x220e, "BCM20702A1" }, /* 001.002.014 */
  311. { 0x230f, "BCM4354A2" }, /* 001.003.015 */
  312. { 0x4106, "BCM4335B0" }, /* 002.001.006 */
  313. { 0x410e, "BCM20702B0" }, /* 002.001.014 */
  314. { 0x6109, "BCM4335C0" }, /* 003.001.009 */
  315. { 0x610c, "BCM4354" }, /* 003.001.012 */
  316. { }
  317. };
  318. int btbcm_setup_patchram(struct hci_dev *hdev)
  319. {
  320. char fw_name[64];
  321. const struct firmware *fw;
  322. u16 subver, rev, pid, vid;
  323. const char *hw_name = NULL;
  324. struct sk_buff *skb;
  325. struct hci_rp_read_local_version *ver;
  326. int i, err;
  327. /* Reset */
  328. err = btbcm_reset(hdev);
  329. if (err)
  330. return err;
  331. /* Read Local Version Info */
  332. skb = btbcm_read_local_version(hdev);
  333. if (IS_ERR(skb))
  334. return PTR_ERR(skb);
  335. ver = (struct hci_rp_read_local_version *)skb->data;
  336. rev = le16_to_cpu(ver->hci_rev);
  337. subver = le16_to_cpu(ver->lmp_subver);
  338. kfree_skb(skb);
  339. /* Read Verbose Config Version Info */
  340. skb = btbcm_read_verbose_config(hdev);
  341. if (IS_ERR(skb))
  342. return PTR_ERR(skb);
  343. BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
  344. kfree_skb(skb);
  345. /* Read Local Name */
  346. skb = btbcm_read_local_name(hdev);
  347. if (IS_ERR(skb))
  348. return PTR_ERR(skb);
  349. BT_INFO("%s: %s", hdev->name, (char *)(skb->data + 1));
  350. kfree_skb(skb);
  351. switch ((rev & 0xf000) >> 12) {
  352. case 0:
  353. case 3:
  354. for (i = 0; bcm_uart_subver_table[i].name; i++) {
  355. if (subver == bcm_uart_subver_table[i].subver) {
  356. hw_name = bcm_uart_subver_table[i].name;
  357. break;
  358. }
  359. }
  360. snprintf(fw_name, sizeof(fw_name), "brcm/%s.hcd",
  361. hw_name ? : "BCM");
  362. break;
  363. case 1:
  364. case 2:
  365. /* Read USB Product Info */
  366. skb = btbcm_read_usb_product(hdev);
  367. if (IS_ERR(skb))
  368. return PTR_ERR(skb);
  369. vid = get_unaligned_le16(skb->data + 1);
  370. pid = get_unaligned_le16(skb->data + 3);
  371. kfree_skb(skb);
  372. for (i = 0; bcm_usb_subver_table[i].name; i++) {
  373. if (subver == bcm_usb_subver_table[i].subver) {
  374. hw_name = bcm_usb_subver_table[i].name;
  375. break;
  376. }
  377. }
  378. snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
  379. hw_name ? : "BCM", vid, pid);
  380. break;
  381. default:
  382. return 0;
  383. }
  384. BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
  385. hw_name ? : "BCM", (subver & 0xe000) >> 13,
  386. (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
  387. err = request_firmware(&fw, fw_name, &hdev->dev);
  388. if (err < 0) {
  389. BT_INFO("%s: BCM: Patch %s not found", hdev->name, fw_name);
  390. return 0;
  391. }
  392. btbcm_patchram(hdev, fw);
  393. release_firmware(fw);
  394. /* Reset */
  395. err = btbcm_reset(hdev);
  396. if (err)
  397. return err;
  398. /* Read Local Version Info */
  399. skb = btbcm_read_local_version(hdev);
  400. if (IS_ERR(skb))
  401. return PTR_ERR(skb);
  402. ver = (struct hci_rp_read_local_version *)skb->data;
  403. rev = le16_to_cpu(ver->hci_rev);
  404. subver = le16_to_cpu(ver->lmp_subver);
  405. kfree_skb(skb);
  406. BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
  407. hw_name ? : "BCM", (subver & 0xe000) >> 13,
  408. (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
  409. /* Read Local Name */
  410. skb = btbcm_read_local_name(hdev);
  411. if (IS_ERR(skb))
  412. return PTR_ERR(skb);
  413. BT_INFO("%s: %s", hdev->name, (char *)(skb->data + 1));
  414. kfree_skb(skb);
  415. btbcm_check_bdaddr(hdev);
  416. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
  420. int btbcm_setup_apple(struct hci_dev *hdev)
  421. {
  422. struct sk_buff *skb;
  423. int err;
  424. /* Reset */
  425. err = btbcm_reset(hdev);
  426. if (err)
  427. return err;
  428. /* Read Verbose Config Version Info */
  429. skb = btbcm_read_verbose_config(hdev);
  430. if (!IS_ERR(skb)) {
  431. BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name,
  432. skb->data[1], get_unaligned_le16(skb->data + 5));
  433. kfree_skb(skb);
  434. }
  435. /* Read USB Product Info */
  436. skb = btbcm_read_usb_product(hdev);
  437. if (!IS_ERR(skb)) {
  438. BT_INFO("%s: BCM: product %4.4x:%4.4x", hdev->name,
  439. get_unaligned_le16(skb->data + 1),
  440. get_unaligned_le16(skb->data + 3));
  441. kfree_skb(skb);
  442. }
  443. /* Read Local Name */
  444. skb = btbcm_read_local_name(hdev);
  445. if (!IS_ERR(skb)) {
  446. BT_INFO("%s: %s", hdev->name, (char *)(skb->data + 1));
  447. kfree_skb(skb);
  448. }
  449. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  450. return 0;
  451. }
  452. EXPORT_SYMBOL_GPL(btbcm_setup_apple);
  453. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  454. MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
  455. MODULE_VERSION(VERSION);
  456. MODULE_LICENSE("GPL");