btqca.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Bluetooth supports for Qualcomm Atheros chips
  3. *
  4. * Copyright (c) 2015 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. static int rome_patch_ver_req(struct hci_dev *hdev, u32 *rome_version)
  27. {
  28. struct sk_buff *skb;
  29. struct edl_event_hdr *edl;
  30. struct rome_version *ver;
  31. char cmd;
  32. int err = 0;
  33. BT_DBG("%s: ROME Patch Version Request", hdev->name);
  34. cmd = EDL_PATCH_VER_REQ_CMD;
  35. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN,
  36. &cmd, HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  37. if (IS_ERR(skb)) {
  38. err = PTR_ERR(skb);
  39. BT_ERR("%s: Failed to read version of ROME (%d)", hdev->name,
  40. err);
  41. return err;
  42. }
  43. if (skb->len != sizeof(*edl) + sizeof(*ver)) {
  44. BT_ERR("%s: Version size mismatch len %d", hdev->name,
  45. skb->len);
  46. err = -EILSEQ;
  47. goto out;
  48. }
  49. edl = (struct edl_event_hdr *)(skb->data);
  50. if (!edl || !edl->data) {
  51. BT_ERR("%s: TLV with no header or no data", hdev->name);
  52. err = -EILSEQ;
  53. goto out;
  54. }
  55. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  56. edl->rtype != EDL_APP_VER_RES_EVT) {
  57. BT_ERR("%s: Wrong packet received %d %d", hdev->name,
  58. edl->cresp, edl->rtype);
  59. err = -EIO;
  60. goto out;
  61. }
  62. ver = (struct rome_version *)(edl->data);
  63. BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id));
  64. BT_DBG("%s: Patch :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver));
  65. BT_DBG("%s: ROM :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver));
  66. BT_DBG("%s: SOC :0x%08x", hdev->name, le32_to_cpu(ver->soc_id));
  67. /* ROME chipset version can be decided by patch and SoC
  68. * version, combination with upper 2 bytes from SoC
  69. * and lower 2 bytes from patch will be used.
  70. */
  71. *rome_version = (le32_to_cpu(ver->soc_id) << 16) |
  72. (le16_to_cpu(ver->rome_ver) & 0x0000ffff);
  73. out:
  74. kfree_skb(skb);
  75. return err;
  76. }
  77. static int rome_reset(struct hci_dev *hdev)
  78. {
  79. struct sk_buff *skb;
  80. int err;
  81. BT_DBG("%s: ROME HCI_RESET", hdev->name);
  82. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  83. if (IS_ERR(skb)) {
  84. err = PTR_ERR(skb);
  85. BT_ERR("%s: Reset failed (%d)", hdev->name, err);
  86. return err;
  87. }
  88. kfree_skb(skb);
  89. return 0;
  90. }
  91. static void rome_tlv_check_data(struct rome_config *config,
  92. const struct firmware *fw)
  93. {
  94. const u8 *data;
  95. u32 type_len;
  96. u16 tag_id, tag_len;
  97. int idx, length;
  98. struct tlv_type_hdr *tlv;
  99. struct tlv_type_patch *tlv_patch;
  100. struct tlv_type_nvm *tlv_nvm;
  101. tlv = (struct tlv_type_hdr *)fw->data;
  102. type_len = le32_to_cpu(tlv->type_len);
  103. length = (type_len >> 8) & 0x00ffffff;
  104. BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
  105. BT_DBG("Length\t\t : %d bytes", length);
  106. switch (config->type) {
  107. case TLV_TYPE_PATCH:
  108. tlv_patch = (struct tlv_type_patch *)tlv->data;
  109. BT_DBG("Total Length\t\t : %d bytes",
  110. le32_to_cpu(tlv_patch->total_size));
  111. BT_DBG("Patch Data Length\t : %d bytes",
  112. le32_to_cpu(tlv_patch->data_length));
  113. BT_DBG("Signing Format Version : 0x%x",
  114. tlv_patch->format_version);
  115. BT_DBG("Signature Algorithm\t : 0x%x",
  116. tlv_patch->signature);
  117. BT_DBG("Reserved\t\t : 0x%x",
  118. le16_to_cpu(tlv_patch->reserved1));
  119. BT_DBG("Product ID\t\t : 0x%04x",
  120. le16_to_cpu(tlv_patch->product_id));
  121. BT_DBG("Rom Build Version\t : 0x%04x",
  122. le16_to_cpu(tlv_patch->rom_build));
  123. BT_DBG("Patch Version\t\t : 0x%04x",
  124. le16_to_cpu(tlv_patch->patch_version));
  125. BT_DBG("Reserved\t\t : 0x%x",
  126. le16_to_cpu(tlv_patch->reserved2));
  127. BT_DBG("Patch Entry Address\t : 0x%x",
  128. le32_to_cpu(tlv_patch->entry));
  129. break;
  130. case TLV_TYPE_NVM:
  131. idx = 0;
  132. data = tlv->data;
  133. while (idx < length) {
  134. tlv_nvm = (struct tlv_type_nvm *)(data + idx);
  135. tag_id = le16_to_cpu(tlv_nvm->tag_id);
  136. tag_len = le16_to_cpu(tlv_nvm->tag_len);
  137. /* Update NVM tags as needed */
  138. switch (tag_id) {
  139. case EDL_TAG_ID_HCI:
  140. /* HCI transport layer parameters
  141. * enabling software inband sleep
  142. * onto controller side.
  143. */
  144. tlv_nvm->data[0] |= 0x80;
  145. /* UART Baud Rate */
  146. tlv_nvm->data[2] = config->user_baud_rate;
  147. break;
  148. case EDL_TAG_ID_DEEP_SLEEP:
  149. /* Sleep enable mask
  150. * enabling deep sleep feature on controller.
  151. */
  152. tlv_nvm->data[0] |= 0x01;
  153. break;
  154. }
  155. idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len);
  156. }
  157. break;
  158. default:
  159. BT_ERR("Unknown TLV type %d", config->type);
  160. break;
  161. }
  162. }
  163. static int rome_tlv_send_segment(struct hci_dev *hdev, int idx, int seg_size,
  164. const u8 *data)
  165. {
  166. struct sk_buff *skb;
  167. struct edl_event_hdr *edl;
  168. struct tlv_seg_resp *tlv_resp;
  169. u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2];
  170. int err = 0;
  171. BT_DBG("%s: Download segment #%d size %d", hdev->name, idx, seg_size);
  172. cmd[0] = EDL_PATCH_TLV_REQ_CMD;
  173. cmd[1] = seg_size;
  174. memcpy(cmd + 2, data, seg_size);
  175. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd,
  176. HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  177. if (IS_ERR(skb)) {
  178. err = PTR_ERR(skb);
  179. BT_ERR("%s: Failed to send TLV segment (%d)", hdev->name, err);
  180. return err;
  181. }
  182. if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) {
  183. BT_ERR("%s: TLV response size mismatch", hdev->name);
  184. err = -EILSEQ;
  185. goto out;
  186. }
  187. edl = (struct edl_event_hdr *)(skb->data);
  188. if (!edl || !edl->data) {
  189. BT_ERR("%s: TLV with no header or no data", hdev->name);
  190. err = -EILSEQ;
  191. goto out;
  192. }
  193. tlv_resp = (struct tlv_seg_resp *)(edl->data);
  194. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  195. edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) {
  196. BT_ERR("%s: TLV with error stat 0x%x rtype 0x%x (0x%x)",
  197. hdev->name, edl->cresp, edl->rtype, tlv_resp->result);
  198. err = -EIO;
  199. }
  200. out:
  201. kfree_skb(skb);
  202. return err;
  203. }
  204. static int rome_tlv_download_request(struct hci_dev *hdev,
  205. const struct firmware *fw)
  206. {
  207. const u8 *buffer, *data;
  208. int total_segment, remain_size;
  209. int ret, i;
  210. if (!fw || !fw->data)
  211. return -EINVAL;
  212. total_segment = fw->size / MAX_SIZE_PER_TLV_SEGMENT;
  213. remain_size = fw->size % MAX_SIZE_PER_TLV_SEGMENT;
  214. BT_DBG("%s: Total segment num %d remain size %d total size %zu",
  215. hdev->name, total_segment, remain_size, fw->size);
  216. data = fw->data;
  217. for (i = 0; i < total_segment; i++) {
  218. buffer = data + i * MAX_SIZE_PER_TLV_SEGMENT;
  219. ret = rome_tlv_send_segment(hdev, i, MAX_SIZE_PER_TLV_SEGMENT,
  220. buffer);
  221. if (ret < 0)
  222. return -EIO;
  223. }
  224. if (remain_size) {
  225. buffer = data + total_segment * MAX_SIZE_PER_TLV_SEGMENT;
  226. ret = rome_tlv_send_segment(hdev, total_segment, remain_size,
  227. buffer);
  228. if (ret < 0)
  229. return -EIO;
  230. }
  231. return 0;
  232. }
  233. static int rome_download_firmware(struct hci_dev *hdev,
  234. struct rome_config *config)
  235. {
  236. const struct firmware *fw;
  237. int ret;
  238. BT_INFO("%s: ROME Downloading %s", hdev->name, config->fwname);
  239. ret = request_firmware(&fw, config->fwname, &hdev->dev);
  240. if (ret) {
  241. BT_ERR("%s: Failed to request file: %s (%d)", hdev->name,
  242. config->fwname, ret);
  243. return ret;
  244. }
  245. rome_tlv_check_data(config, fw);
  246. ret = rome_tlv_download_request(hdev, fw);
  247. if (ret) {
  248. BT_ERR("%s: Failed to download file: %s (%d)", hdev->name,
  249. config->fwname, ret);
  250. }
  251. release_firmware(fw);
  252. return ret;
  253. }
  254. int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  255. {
  256. struct sk_buff *skb;
  257. u8 cmd[9];
  258. int err;
  259. cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD;
  260. cmd[1] = 0x02; /* TAG ID */
  261. cmd[2] = sizeof(bdaddr_t); /* size */
  262. memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t));
  263. skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd,
  264. HCI_VENDOR_PKT, HCI_INIT_TIMEOUT);
  265. if (IS_ERR(skb)) {
  266. err = PTR_ERR(skb);
  267. BT_ERR("%s: Change address command failed (%d)",
  268. hdev->name, err);
  269. return err;
  270. }
  271. kfree_skb(skb);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome);
  275. int qca_uart_setup_rome(struct hci_dev *hdev, uint8_t baudrate)
  276. {
  277. u32 rome_ver = 0;
  278. struct rome_config config;
  279. int err;
  280. BT_DBG("%s: ROME setup on UART", hdev->name);
  281. config.user_baud_rate = baudrate;
  282. /* Get ROME version information */
  283. err = rome_patch_ver_req(hdev, &rome_ver);
  284. if (err < 0 || rome_ver == 0) {
  285. BT_ERR("%s: Failed to get version 0x%x", hdev->name, err);
  286. return err;
  287. }
  288. BT_INFO("%s: ROME controller version 0x%08x", hdev->name, rome_ver);
  289. /* Download rampatch file */
  290. config.type = TLV_TYPE_PATCH;
  291. snprintf(config.fwname, sizeof(config.fwname), "qca/rampatch_%08x.bin",
  292. rome_ver);
  293. err = rome_download_firmware(hdev, &config);
  294. if (err < 0) {
  295. BT_ERR("%s: Failed to download patch (%d)", hdev->name, err);
  296. return err;
  297. }
  298. /* Download NVM configuration */
  299. config.type = TLV_TYPE_NVM;
  300. snprintf(config.fwname, sizeof(config.fwname), "qca/nvm_%08x.bin",
  301. rome_ver);
  302. err = rome_download_firmware(hdev, &config);
  303. if (err < 0) {
  304. BT_ERR("%s: Failed to download NVM (%d)", hdev->name, err);
  305. return err;
  306. }
  307. /* Perform HCI reset */
  308. err = rome_reset(hdev);
  309. if (err < 0) {
  310. BT_ERR("%s: Failed to run HCI_RESET (%d)", hdev->name, err);
  311. return err;
  312. }
  313. BT_INFO("%s: ROME setup on UART is completed", hdev->name);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(qca_uart_setup_rome);
  317. MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>");
  318. MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION);
  319. MODULE_VERSION(VERSION);
  320. MODULE_LICENSE("GPL");