hci_mrvl.c 8.6 KB

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