hci_mrvl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. *
  3. * Bluetooth HCI UART driver for marvell devices
  4. *
  5. * Copyright (C) 2016 Marvell International Ltd.
  6. * Copyright (C) 2016 Intel Corporation
  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/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/firmware.h>
  27. #include <linux/module.h>
  28. #include <linux/tty.h>
  29. #include <net/bluetooth/bluetooth.h>
  30. #include <net/bluetooth/hci_core.h>
  31. #include "hci_uart.h"
  32. #define HCI_FW_REQ_PKT 0xA5
  33. #define HCI_CHIP_VER_PKT 0xAA
  34. #define MRVL_ACK 0x5A
  35. #define MRVL_NAK 0xBF
  36. #define MRVL_RAW_DATA 0x1F
  37. enum {
  38. STATE_CHIP_VER_PENDING,
  39. STATE_FW_REQ_PENDING,
  40. };
  41. struct mrvl_data {
  42. struct sk_buff *rx_skb;
  43. struct sk_buff_head txq;
  44. struct sk_buff_head rawq;
  45. unsigned long flags;
  46. unsigned int tx_len;
  47. u8 id, rev;
  48. };
  49. struct hci_mrvl_pkt {
  50. __le16 lhs;
  51. __le16 rhs;
  52. } __packed;
  53. #define HCI_MRVL_PKT_SIZE 4
  54. static int mrvl_open(struct hci_uart *hu)
  55. {
  56. struct mrvl_data *mrvl;
  57. BT_DBG("hu %p", hu);
  58. if (!hci_uart_has_flow_control(hu))
  59. return -EOPNOTSUPP;
  60. mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL);
  61. if (!mrvl)
  62. return -ENOMEM;
  63. skb_queue_head_init(&mrvl->txq);
  64. skb_queue_head_init(&mrvl->rawq);
  65. set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
  66. hu->priv = mrvl;
  67. return 0;
  68. }
  69. static int mrvl_close(struct hci_uart *hu)
  70. {
  71. struct mrvl_data *mrvl = hu->priv;
  72. BT_DBG("hu %p", hu);
  73. skb_queue_purge(&mrvl->txq);
  74. skb_queue_purge(&mrvl->rawq);
  75. kfree_skb(mrvl->rx_skb);
  76. kfree(mrvl);
  77. hu->priv = NULL;
  78. return 0;
  79. }
  80. static int mrvl_flush(struct hci_uart *hu)
  81. {
  82. struct mrvl_data *mrvl = hu->priv;
  83. BT_DBG("hu %p", hu);
  84. skb_queue_purge(&mrvl->txq);
  85. skb_queue_purge(&mrvl->rawq);
  86. return 0;
  87. }
  88. static struct sk_buff *mrvl_dequeue(struct hci_uart *hu)
  89. {
  90. struct mrvl_data *mrvl = hu->priv;
  91. struct sk_buff *skb;
  92. skb = skb_dequeue(&mrvl->txq);
  93. if (!skb) {
  94. /* Any raw data ? */
  95. skb = skb_dequeue(&mrvl->rawq);
  96. } else {
  97. /* Prepend skb with frame type */
  98. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  99. }
  100. return skb;
  101. }
  102. static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  103. {
  104. struct mrvl_data *mrvl = hu->priv;
  105. skb_queue_tail(&mrvl->txq, skb);
  106. return 0;
  107. }
  108. static void mrvl_send_ack(struct hci_uart *hu, unsigned char type)
  109. {
  110. struct mrvl_data *mrvl = hu->priv;
  111. struct sk_buff *skb;
  112. /* No H4 payload, only 1 byte header */
  113. skb = bt_skb_alloc(0, GFP_ATOMIC);
  114. if (!skb) {
  115. bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet");
  116. return;
  117. }
  118. hci_skb_pkt_type(skb) = type;
  119. skb_queue_tail(&mrvl->txq, skb);
  120. hci_uart_tx_wakeup(hu);
  121. }
  122. static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb)
  123. {
  124. struct hci_mrvl_pkt *pkt = (void *)skb->data;
  125. struct hci_uart *hu = hci_get_drvdata(hdev);
  126. struct mrvl_data *mrvl = hu->priv;
  127. int ret = 0;
  128. if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
  129. bt_dev_err(hdev, "Corrupted mrvl header");
  130. mrvl_send_ack(hu, MRVL_NAK);
  131. ret = -EINVAL;
  132. goto done;
  133. }
  134. mrvl_send_ack(hu, MRVL_ACK);
  135. if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) {
  136. bt_dev_err(hdev, "Received unexpected firmware request");
  137. ret = -EINVAL;
  138. goto done;
  139. }
  140. mrvl->tx_len = le16_to_cpu(pkt->lhs);
  141. clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
  142. smp_mb__after_atomic();
  143. wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING);
  144. done:
  145. kfree_skb(skb);
  146. return ret;
  147. }
  148. static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb)
  149. {
  150. struct hci_mrvl_pkt *pkt = (void *)skb->data;
  151. struct hci_uart *hu = hci_get_drvdata(hdev);
  152. struct mrvl_data *mrvl = hu->priv;
  153. u16 version = le16_to_cpu(pkt->lhs);
  154. int ret = 0;
  155. if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
  156. bt_dev_err(hdev, "Corrupted mrvl header");
  157. mrvl_send_ack(hu, MRVL_NAK);
  158. ret = -EINVAL;
  159. goto done;
  160. }
  161. mrvl_send_ack(hu, MRVL_ACK);
  162. if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) {
  163. bt_dev_err(hdev, "Received unexpected chip version");
  164. goto done;
  165. }
  166. mrvl->id = version;
  167. mrvl->rev = version >> 8;
  168. bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev);
  169. clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
  170. smp_mb__after_atomic();
  171. wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING);
  172. done:
  173. kfree_skb(skb);
  174. return ret;
  175. }
  176. #define HCI_RECV_CHIP_VER \
  177. .type = HCI_CHIP_VER_PKT, \
  178. .hlen = HCI_MRVL_PKT_SIZE, \
  179. .loff = 0, \
  180. .lsize = 0, \
  181. .maxlen = HCI_MRVL_PKT_SIZE
  182. #define HCI_RECV_FW_REQ \
  183. .type = HCI_FW_REQ_PKT, \
  184. .hlen = HCI_MRVL_PKT_SIZE, \
  185. .loff = 0, \
  186. .lsize = 0, \
  187. .maxlen = HCI_MRVL_PKT_SIZE
  188. static const struct h4_recv_pkt mrvl_recv_pkts[] = {
  189. { H4_RECV_ACL, .recv = hci_recv_frame },
  190. { H4_RECV_SCO, .recv = hci_recv_frame },
  191. { H4_RECV_EVENT, .recv = hci_recv_frame },
  192. { HCI_RECV_FW_REQ, .recv = mrvl_recv_fw_req },
  193. { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver },
  194. };
  195. static int mrvl_recv(struct hci_uart *hu, const void *data, int count)
  196. {
  197. struct mrvl_data *mrvl = hu->priv;
  198. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  199. return -EUNATCH;
  200. mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count,
  201. mrvl_recv_pkts,
  202. ARRAY_SIZE(mrvl_recv_pkts));
  203. if (IS_ERR(mrvl->rx_skb)) {
  204. int err = PTR_ERR(mrvl->rx_skb);
  205. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  206. mrvl->rx_skb = NULL;
  207. return err;
  208. }
  209. return count;
  210. }
  211. static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
  212. {
  213. struct hci_uart *hu = hci_get_drvdata(hdev);
  214. struct mrvl_data *mrvl = hu->priv;
  215. const struct firmware *fw = NULL;
  216. const u8 *fw_ptr, *fw_max;
  217. int err;
  218. err = request_firmware(&fw, name, &hdev->dev);
  219. if (err < 0) {
  220. bt_dev_err(hdev, "Failed to load firmware file %s", name);
  221. return err;
  222. }
  223. fw_ptr = fw->data;
  224. fw_max = fw->data + fw->size;
  225. bt_dev_info(hdev, "Loading %s", name);
  226. set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
  227. while (fw_ptr <= fw_max) {
  228. struct sk_buff *skb;
  229. /* Controller drives the firmware load by sending firmware
  230. * request packets containing the expected fragment size.
  231. */
  232. err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING,
  233. TASK_INTERRUPTIBLE,
  234. msecs_to_jiffies(2000));
  235. if (err == 1) {
  236. bt_dev_err(hdev, "Firmware load interrupted");
  237. err = -EINTR;
  238. break;
  239. } else if (err) {
  240. bt_dev_err(hdev, "Firmware request timeout");
  241. err = -ETIMEDOUT;
  242. break;
  243. }
  244. bt_dev_dbg(hdev, "Firmware request, expecting %d bytes",
  245. mrvl->tx_len);
  246. if (fw_ptr == fw_max) {
  247. /* Controller requests a null size once firmware is
  248. * fully loaded. If controller expects more data, there
  249. * is an issue.
  250. */
  251. if (!mrvl->tx_len) {
  252. bt_dev_info(hdev, "Firmware loading complete");
  253. } else {
  254. bt_dev_err(hdev, "Firmware loading failure");
  255. err = -EINVAL;
  256. }
  257. break;
  258. }
  259. if (fw_ptr + mrvl->tx_len > fw_max) {
  260. mrvl->tx_len = fw_max - fw_ptr;
  261. bt_dev_dbg(hdev, "Adjusting tx_len to %d",
  262. mrvl->tx_len);
  263. }
  264. skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL);
  265. if (!skb) {
  266. bt_dev_err(hdev, "Failed to alloc mem for FW packet");
  267. err = -ENOMEM;
  268. break;
  269. }
  270. bt_cb(skb)->pkt_type = MRVL_RAW_DATA;
  271. skb_put_data(skb, fw_ptr, mrvl->tx_len);
  272. fw_ptr += mrvl->tx_len;
  273. set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
  274. skb_queue_tail(&mrvl->rawq, skb);
  275. hci_uart_tx_wakeup(hu);
  276. }
  277. release_firmware(fw);
  278. return err;
  279. }
  280. static int mrvl_setup(struct hci_uart *hu)
  281. {
  282. int err;
  283. hci_uart_set_flow_control(hu, true);
  284. err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin");
  285. if (err) {
  286. bt_dev_err(hu->hdev, "Unable to download firmware helper");
  287. return -EINVAL;
  288. }
  289. hci_uart_set_baudrate(hu, 3000000);
  290. hci_uart_set_flow_control(hu, false);
  291. err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin");
  292. if (err)
  293. return err;
  294. return 0;
  295. }
  296. static const struct hci_uart_proto mrvl_proto = {
  297. .id = HCI_UART_MRVL,
  298. .name = "Marvell",
  299. .init_speed = 115200,
  300. .open = mrvl_open,
  301. .close = mrvl_close,
  302. .flush = mrvl_flush,
  303. .setup = mrvl_setup,
  304. .recv = mrvl_recv,
  305. .enqueue = mrvl_enqueue,
  306. .dequeue = mrvl_dequeue,
  307. };
  308. int __init mrvl_init(void)
  309. {
  310. return hci_uart_register_proto(&mrvl_proto);
  311. }
  312. int __exit mrvl_deinit(void)
  313. {
  314. return hci_uart_unregister_proto(&mrvl_proto);
  315. }