btqca.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Bluetooth supports for Qualcomm Atheros chips
  3. *
  4. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/firmware.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include "btqca.h"
  25. #define VERSION "0.1"
  26. #define MAX_PATCH_FILE_SIZE (100*1024)
  27. #define MAX_NVM_FILE_SIZE (10*1024)
  28. static int rome_patch_ver_req(struct hci_dev *hdev, u32 *rome_version)
  29. {
  30. struct sk_buff *skb;
  31. struct edl_event_hdr *edl;
  32. struct rome_version *ver;
  33. char cmd;
  34. int err = 0;
  35. BT_DBG("%s: ROME Patch Version Request", hdev->name);
  36. cmd = EDL_PATCH_VER_REQ_CMD;
  37. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN,
  38. &cmd, HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  39. if (IS_ERR(skb)) {
  40. err = PTR_ERR(skb);
  41. BT_ERR("%s: Failed to read version of ROME (%d)", hdev->name,
  42. err);
  43. return err;
  44. }
  45. if (skb->len != sizeof(*edl) + sizeof(*ver)) {
  46. BT_ERR("%s: Version size mismatch len %d", hdev->name,
  47. skb->len);
  48. err = -EILSEQ;
  49. goto out;
  50. }
  51. edl = (struct edl_event_hdr *)(skb->data);
  52. if (!edl) {
  53. BT_ERR("%s: TLV with no header", hdev->name);
  54. err = -EILSEQ;
  55. goto out;
  56. }
  57. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  58. edl->rtype != EDL_APP_VER_RES_EVT) {
  59. BT_ERR("%s: Wrong packet received %d %d", hdev->name,
  60. edl->cresp, edl->rtype);
  61. err = -EIO;
  62. goto out;
  63. }
  64. ver = (struct rome_version *)(edl->data);
  65. BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id));
  66. BT_DBG("%s: Patch :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver));
  67. BT_DBG("%s: ROM :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver));
  68. BT_DBG("%s: SOC :0x%08x", hdev->name, le32_to_cpu(ver->soc_id));
  69. /* ROME chipset version can be decided by patch and SoC
  70. * version, combination with upper 2 bytes from SoC
  71. * and lower 2 bytes from patch will be used.
  72. */
  73. *rome_version = (le32_to_cpu(ver->soc_id) << 16) |
  74. (le16_to_cpu(ver->rome_ver) & 0x0000ffff);
  75. out:
  76. kfree_skb(skb);
  77. return err;
  78. }
  79. static int rome_reset(struct hci_dev *hdev)
  80. {
  81. struct sk_buff *skb;
  82. int err;
  83. BT_DBG("%s: ROME HCI_RESET", hdev->name);
  84. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  85. if (IS_ERR(skb)) {
  86. err = PTR_ERR(skb);
  87. BT_ERR("%s: Reset failed (%d)", hdev->name, err);
  88. return err;
  89. }
  90. kfree_skb(skb);
  91. return 0;
  92. }
  93. static void rome_tlv_check_data(struct rome_config *config,
  94. const struct firmware *fw)
  95. {
  96. const u8 *data;
  97. u32 type_len;
  98. u16 tag_id, tag_len;
  99. int idx, length;
  100. struct tlv_type_hdr *tlv;
  101. struct tlv_type_patch *tlv_patch;
  102. struct tlv_type_nvm *tlv_nvm;
  103. tlv = (struct tlv_type_hdr *)fw->data;
  104. type_len = le32_to_cpu(tlv->type_len);
  105. length = (type_len >> 8) & 0x00ffffff;
  106. BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
  107. BT_DBG("Length\t\t : %d bytes", length);
  108. switch (config->type) {
  109. case TLV_TYPE_PATCH:
  110. tlv_patch = (struct tlv_type_patch *)tlv->data;
  111. BT_DBG("Total Length\t\t : %d bytes",
  112. le32_to_cpu(tlv_patch->total_size));
  113. BT_DBG("Patch Data Length\t : %d bytes",
  114. le32_to_cpu(tlv_patch->data_length));
  115. BT_DBG("Signing Format Version : 0x%x",
  116. tlv_patch->format_version);
  117. BT_DBG("Signature Algorithm\t : 0x%x",
  118. tlv_patch->signature);
  119. BT_DBG("Reserved\t\t : 0x%x",
  120. le16_to_cpu(tlv_patch->reserved1));
  121. BT_DBG("Product ID\t\t : 0x%04x",
  122. le16_to_cpu(tlv_patch->product_id));
  123. BT_DBG("Rom Build Version\t : 0x%04x",
  124. le16_to_cpu(tlv_patch->rom_build));
  125. BT_DBG("Patch Version\t\t : 0x%04x",
  126. le16_to_cpu(tlv_patch->patch_version));
  127. BT_DBG("Reserved\t\t : 0x%x",
  128. le16_to_cpu(tlv_patch->reserved2));
  129. BT_DBG("Patch Entry Address\t : 0x%x",
  130. le32_to_cpu(tlv_patch->entry));
  131. break;
  132. case TLV_TYPE_NVM:
  133. idx = 0;
  134. data = tlv->data;
  135. while (idx < length) {
  136. tlv_nvm = (struct tlv_type_nvm *)(data + idx);
  137. tag_id = le16_to_cpu(tlv_nvm->tag_id);
  138. tag_len = le16_to_cpu(tlv_nvm->tag_len);
  139. /* Update NVM tags as needed */
  140. switch (tag_id) {
  141. case EDL_TAG_ID_HCI:
  142. /* HCI transport layer parameters
  143. * enabling software inband sleep
  144. * onto controller side.
  145. */
  146. tlv_nvm->data[0] |= 0x80;
  147. /* UART Baud Rate */
  148. tlv_nvm->data[2] = config->user_baud_rate;
  149. break;
  150. case EDL_TAG_ID_DEEP_SLEEP:
  151. /* Sleep enable mask
  152. * enabling deep sleep feature on controller.
  153. */
  154. tlv_nvm->data[0] |= 0x01;
  155. break;
  156. }
  157. idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len);
  158. }
  159. break;
  160. default:
  161. BT_ERR("Unknown TLV type %d", config->type);
  162. break;
  163. }
  164. }
  165. static int rome_tlv_send_segment(struct hci_dev *hdev, int idx, int seg_size,
  166. const u8 *data)
  167. {
  168. struct sk_buff *skb;
  169. struct edl_event_hdr *edl;
  170. struct tlv_seg_resp *tlv_resp;
  171. u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2];
  172. int err = 0;
  173. BT_DBG("%s: Download segment #%d size %d", hdev->name, idx, seg_size);
  174. cmd[0] = EDL_PATCH_TLV_REQ_CMD;
  175. cmd[1] = seg_size;
  176. memcpy(cmd + 2, data, seg_size);
  177. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd,
  178. HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  179. if (IS_ERR(skb)) {
  180. err = PTR_ERR(skb);
  181. BT_ERR("%s: Failed to send TLV segment (%d)", hdev->name, err);
  182. return err;
  183. }
  184. if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) {
  185. BT_ERR("%s: TLV response size mismatch", hdev->name);
  186. err = -EILSEQ;
  187. goto out;
  188. }
  189. edl = (struct edl_event_hdr *)(skb->data);
  190. if (!edl) {
  191. BT_ERR("%s: TLV with no header", hdev->name);
  192. err = -EILSEQ;
  193. goto out;
  194. }
  195. tlv_resp = (struct tlv_seg_resp *)(edl->data);
  196. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  197. edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) {
  198. BT_ERR("%s: TLV with error stat 0x%x rtype 0x%x (0x%x)",
  199. hdev->name, edl->cresp, edl->rtype, tlv_resp->result);
  200. err = -EIO;
  201. }
  202. out:
  203. kfree_skb(skb);
  204. return err;
  205. }
  206. static int rome_tlv_download_request(struct hci_dev *hdev,
  207. const struct firmware *fw)
  208. {
  209. const u8 *buffer, *data;
  210. int total_segment, remain_size;
  211. int ret, i;
  212. if (!fw || !fw->data)
  213. return -EINVAL;
  214. total_segment = fw->size / MAX_SIZE_PER_TLV_SEGMENT;
  215. remain_size = fw->size % MAX_SIZE_PER_TLV_SEGMENT;
  216. BT_DBG("%s: Total segment num %d remain size %d total size %zu",
  217. hdev->name, total_segment, remain_size, fw->size);
  218. data = fw->data;
  219. for (i = 0; i < total_segment; i++) {
  220. buffer = data + i * MAX_SIZE_PER_TLV_SEGMENT;
  221. ret = rome_tlv_send_segment(hdev, i, MAX_SIZE_PER_TLV_SEGMENT,
  222. buffer);
  223. if (ret < 0)
  224. return -EIO;
  225. }
  226. if (remain_size) {
  227. buffer = data + total_segment * MAX_SIZE_PER_TLV_SEGMENT;
  228. ret = rome_tlv_send_segment(hdev, total_segment, remain_size,
  229. buffer);
  230. if (ret < 0)
  231. return -EIO;
  232. }
  233. return 0;
  234. }
  235. static int rome_download_firmware(struct hci_dev *hdev,
  236. struct rome_config *config)
  237. {
  238. const struct firmware *fw;
  239. u32 type_len, length;
  240. struct tlv_type_hdr *tlv;
  241. int ret;
  242. BT_INFO("%s: ROME Downloading file: %s", hdev->name, config->fwname);
  243. ret = request_firmware(&fw, config->fwname, &hdev->dev);
  244. if (ret || !fw || !fw->data || fw->size <= 0) {
  245. BT_ERR("Failed to request file: err = (%d)", ret);
  246. ret = ret ? ret : -EINVAL;
  247. return ret;
  248. }
  249. if (config->type != TLV_TYPE_NVM &&
  250. config->type != TLV_TYPE_PATCH) {
  251. ret = -EINVAL;
  252. BT_ERR("TLV_NVM dload: wrong config type selected");
  253. goto exit;
  254. }
  255. if (config->type == TLV_TYPE_PATCH &&
  256. (fw->size > MAX_PATCH_FILE_SIZE)) {
  257. ret = -EINVAL;
  258. BT_ERR("TLV_PATCH dload: wrong patch file sizes");
  259. goto exit;
  260. } else if (config->type == TLV_TYPE_NVM &&
  261. (fw->size > MAX_NVM_FILE_SIZE)) {
  262. ret = -EINVAL;
  263. BT_ERR("TLV_NVM dload: wrong NVM file sizes");
  264. goto exit;
  265. }
  266. if (fw->size < sizeof(struct tlv_type_hdr)) {
  267. ret = -EINVAL;
  268. BT_ERR("Firware size smaller to fit minimum value");
  269. goto exit;
  270. }
  271. tlv = (struct tlv_type_hdr *)fw->data;
  272. type_len = le32_to_cpu(tlv->type_len);
  273. length = (type_len >> 8) & 0x00ffffff;
  274. if (fw->size - 4 != length) {
  275. ret = -EINVAL;
  276. BT_ERR("Requested size not matching size in header");
  277. goto exit;
  278. }
  279. rome_tlv_check_data(config, fw);
  280. ret = rome_tlv_download_request(hdev, fw);
  281. if (ret) {
  282. BT_ERR("Failed to download FW: error = (%d)", ret);
  283. }
  284. exit:
  285. release_firmware(fw);
  286. return ret;
  287. }
  288. int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  289. {
  290. struct sk_buff *skb;
  291. u8 cmd[9];
  292. int err;
  293. cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD;
  294. /* Set the TAG ID of 0x02 for NVM set and size of tag */
  295. cmd[1] = 0x02;
  296. cmd[2] = sizeof(bdaddr_t);
  297. memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t));
  298. skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd,
  299. HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  300. if (IS_ERR(skb)) {
  301. err = PTR_ERR(skb);
  302. BT_ERR("%s: Change address command failed (%d)",
  303. hdev->name, err);
  304. return err;
  305. }
  306. kfree_skb(skb);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome);
  310. int qca_uart_setup_rome(struct hci_dev *hdev, uint8_t baudrate)
  311. {
  312. u32 rome_ver = 0;
  313. struct rome_config config;
  314. int err;
  315. BT_DBG("%s: ROME setup on UART", hdev->name);
  316. config.user_baud_rate = baudrate;
  317. /* Get ROME version information */
  318. err = rome_patch_ver_req(hdev, &rome_ver);
  319. if (err < 0 || rome_ver == 0) {
  320. BT_ERR("%s: Failed to get version 0x%x", hdev->name, err);
  321. return err;
  322. }
  323. BT_INFO("%s: ROME controller version 0x%08x", hdev->name, rome_ver);
  324. /* Download rampatch file */
  325. config.type = TLV_TYPE_PATCH;
  326. snprintf(config.fwname, sizeof(config.fwname), "qca/rampatch_%08x.bin",
  327. rome_ver);
  328. err = rome_download_firmware(hdev, &config);
  329. if (err < 0) {
  330. BT_ERR("%s: Failed to download patch (%d)", hdev->name, err);
  331. return err;
  332. }
  333. /* Give the controller some time to get ready to receive the NVM */
  334. msleep(10);
  335. /* Download NVM configuration */
  336. config.type = TLV_TYPE_NVM;
  337. snprintf(config.fwname, sizeof(config.fwname), "qca/nvm_%08x.bin",
  338. rome_ver);
  339. err = rome_download_firmware(hdev, &config);
  340. if (err < 0) {
  341. BT_ERR("%s: Failed to download NVM (%d)", hdev->name, err);
  342. return err;
  343. }
  344. /* Perform HCI reset */
  345. err = rome_reset(hdev);
  346. if (err < 0) {
  347. BT_ERR("%s: Failed to run HCI_RESET (%d)", hdev->name, err);
  348. return err;
  349. }
  350. BT_INFO("%s: ROME setup on UART is completed", hdev->name);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(qca_uart_setup_rome);
  354. MODULE_AUTHOR("Ben Young Tae Kim <[email protected]>");
  355. MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION);
  356. MODULE_VERSION(VERSION);
  357. MODULE_LICENSE("GPL");