btmtkuart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 MediaTek Inc.
  3. /*
  4. * Bluetooth support for MediaTek serial devices
  5. *
  6. * Author: Sean Wang <sean.wang@mediatek.com>
  7. *
  8. */
  9. #include <asm/unaligned.h>
  10. #include <linux/atomic.h>
  11. #include <linux/clk.h>
  12. #include <linux/firmware.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/serdev.h>
  18. #include <linux/skbuff.h>
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include "h4_recv.h"
  22. #define VERSION "0.1"
  23. #define FIRMWARE_MT7622 "mediatek/mt7622pr2h.bin"
  24. #define MTK_STP_TLR_SIZE 2
  25. #define BTMTKUART_TX_STATE_ACTIVE 1
  26. #define BTMTKUART_TX_STATE_WAKEUP 2
  27. #define BTMTKUART_TX_WAIT_VND_EVT 3
  28. enum {
  29. MTK_WMT_PATCH_DWNLD = 0x1,
  30. MTK_WMT_FUNC_CTRL = 0x6,
  31. MTK_WMT_RST = 0x7
  32. };
  33. struct mtk_stp_hdr {
  34. u8 prefix;
  35. __be16 dlen;
  36. u8 cs;
  37. } __packed;
  38. struct mtk_wmt_hdr {
  39. u8 dir;
  40. u8 op;
  41. __le16 dlen;
  42. u8 flag;
  43. } __packed;
  44. struct mtk_hci_wmt_cmd {
  45. struct mtk_wmt_hdr hdr;
  46. u8 data[256];
  47. } __packed;
  48. struct btmtkuart_dev {
  49. struct hci_dev *hdev;
  50. struct serdev_device *serdev;
  51. struct clk *clk;
  52. struct work_struct tx_work;
  53. unsigned long tx_state;
  54. struct sk_buff_head txq;
  55. struct sk_buff *rx_skb;
  56. u8 stp_pad[6];
  57. u8 stp_cursor;
  58. u16 stp_dlen;
  59. };
  60. static int mtk_hci_wmt_sync(struct hci_dev *hdev, u8 op, u8 flag, u16 plen,
  61. const void *param)
  62. {
  63. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  64. struct mtk_hci_wmt_cmd wc;
  65. struct mtk_wmt_hdr *hdr;
  66. u32 hlen;
  67. int err;
  68. hlen = sizeof(*hdr) + plen;
  69. if (hlen > 255)
  70. return -EINVAL;
  71. hdr = (struct mtk_wmt_hdr *)&wc;
  72. hdr->dir = 1;
  73. hdr->op = op;
  74. hdr->dlen = cpu_to_le16(plen + 1);
  75. hdr->flag = flag;
  76. memcpy(wc.data, param, plen);
  77. set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
  78. err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
  79. if (err < 0) {
  80. clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
  81. return err;
  82. }
  83. /* The vendor specific WMT commands are all answered by a vendor
  84. * specific event and will not have the Command Status or Command
  85. * Complete as with usual HCI command flow control.
  86. *
  87. * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
  88. * state to be cleared. The driver speicfic event receive routine
  89. * will clear that state and with that indicate completion of the
  90. * WMT command.
  91. */
  92. err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
  93. TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
  94. if (err == -EINTR) {
  95. bt_dev_err(hdev, "Execution of wmt command interrupted");
  96. clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
  97. return err;
  98. }
  99. if (err) {
  100. bt_dev_err(hdev, "Execution of wmt command timed out");
  101. clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
  102. return -ETIMEDOUT;
  103. }
  104. return 0;
  105. }
  106. static int mtk_setup_fw(struct hci_dev *hdev)
  107. {
  108. const struct firmware *fw;
  109. const u8 *fw_ptr;
  110. size_t fw_size;
  111. int err, dlen;
  112. u8 flag;
  113. err = request_firmware(&fw, FIRMWARE_MT7622, &hdev->dev);
  114. if (err < 0) {
  115. bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
  116. return err;
  117. }
  118. fw_ptr = fw->data;
  119. fw_size = fw->size;
  120. /* The size of patch header is 30 bytes, should be skip */
  121. if (fw_size < 30) {
  122. err = -EINVAL;
  123. goto free_fw;
  124. }
  125. fw_size -= 30;
  126. fw_ptr += 30;
  127. flag = 1;
  128. while (fw_size > 0) {
  129. dlen = min_t(int, 250, fw_size);
  130. /* Tell device the position in sequence */
  131. if (fw_size - dlen <= 0)
  132. flag = 3;
  133. else if (fw_size < fw->size - 30)
  134. flag = 2;
  135. err = mtk_hci_wmt_sync(hdev, MTK_WMT_PATCH_DWNLD, flag, dlen,
  136. fw_ptr);
  137. if (err < 0) {
  138. bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
  139. err);
  140. break;
  141. }
  142. fw_size -= dlen;
  143. fw_ptr += dlen;
  144. }
  145. free_fw:
  146. release_firmware(fw);
  147. return err;
  148. }
  149. static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  150. {
  151. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  152. struct hci_event_hdr *hdr = (void *)skb->data;
  153. int err;
  154. /* Fix up the vendor event id with 0xff for vendor specific instead
  155. * of 0xe4 so that event send via monitoring socket can be parsed
  156. * properly.
  157. */
  158. if (hdr->evt == 0xe4)
  159. hdr->evt = HCI_EV_VENDOR;
  160. err = hci_recv_frame(hdev, skb);
  161. if (hdr->evt == HCI_EV_VENDOR) {
  162. if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
  163. &bdev->tx_state)) {
  164. /* Barrier to sync with other CPUs */
  165. smp_mb__after_atomic();
  166. wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
  167. }
  168. }
  169. return err;
  170. }
  171. static const struct h4_recv_pkt mtk_recv_pkts[] = {
  172. { H4_RECV_ACL, .recv = hci_recv_frame },
  173. { H4_RECV_SCO, .recv = hci_recv_frame },
  174. { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
  175. };
  176. static void btmtkuart_tx_work(struct work_struct *work)
  177. {
  178. struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
  179. tx_work);
  180. struct serdev_device *serdev = bdev->serdev;
  181. struct hci_dev *hdev = bdev->hdev;
  182. while (1) {
  183. clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
  184. while (1) {
  185. struct sk_buff *skb = skb_dequeue(&bdev->txq);
  186. int len;
  187. if (!skb)
  188. break;
  189. len = serdev_device_write_buf(serdev, skb->data,
  190. skb->len);
  191. hdev->stat.byte_tx += len;
  192. skb_pull(skb, len);
  193. if (skb->len > 0) {
  194. skb_queue_head(&bdev->txq, skb);
  195. break;
  196. }
  197. switch (hci_skb_pkt_type(skb)) {
  198. case HCI_COMMAND_PKT:
  199. hdev->stat.cmd_tx++;
  200. break;
  201. case HCI_ACLDATA_PKT:
  202. hdev->stat.acl_tx++;
  203. break;
  204. case HCI_SCODATA_PKT:
  205. hdev->stat.sco_tx++;
  206. break;
  207. }
  208. kfree_skb(skb);
  209. }
  210. if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
  211. break;
  212. }
  213. clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
  214. }
  215. static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
  216. {
  217. if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
  218. set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
  219. schedule_work(&bdev->tx_work);
  220. }
  221. static const unsigned char *
  222. mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
  223. int *sz_h4)
  224. {
  225. struct mtk_stp_hdr *shdr;
  226. /* The cursor is reset when all the data of STP is consumed out */
  227. if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
  228. bdev->stp_cursor = 0;
  229. /* Filling pad until all STP info is obtained */
  230. while (bdev->stp_cursor < 6 && count > 0) {
  231. bdev->stp_pad[bdev->stp_cursor] = *data;
  232. bdev->stp_cursor++;
  233. data++;
  234. count--;
  235. }
  236. /* Retrieve STP info and have a sanity check */
  237. if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
  238. shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
  239. bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
  240. /* Resync STP when unexpected data is being read */
  241. if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
  242. bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
  243. shdr->prefix, bdev->stp_dlen);
  244. bdev->stp_cursor = 2;
  245. bdev->stp_dlen = 0;
  246. }
  247. }
  248. /* Directly quit when there's no data found for H4 can process */
  249. if (count <= 0)
  250. return NULL;
  251. /* Tranlate to how much the size of data H4 can handle so far */
  252. *sz_h4 = min_t(int, count, bdev->stp_dlen);
  253. /* Update the remaining size of STP packet */
  254. bdev->stp_dlen -= *sz_h4;
  255. /* Data points to STP payload which can be handled by H4 */
  256. return data;
  257. }
  258. static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
  259. {
  260. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  261. const unsigned char *p_left = data, *p_h4;
  262. int sz_left = count, sz_h4, adv;
  263. int err;
  264. while (sz_left > 0) {
  265. /* The serial data received from MT7622 BT controller is
  266. * at all time padded around with the STP header and tailer.
  267. *
  268. * A full STP packet is looking like
  269. * -----------------------------------
  270. * | STP header | H:4 | STP tailer |
  271. * -----------------------------------
  272. * but it doesn't guarantee to contain a full H:4 packet which
  273. * means that it's possible for multiple STP packets forms a
  274. * full H:4 packet that means extra STP header + length doesn't
  275. * indicate a full H:4 frame, things can fragment. Whose length
  276. * recorded in STP header just shows up the most length the
  277. * H:4 engine can handle currently.
  278. */
  279. p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
  280. if (!p_h4)
  281. break;
  282. adv = p_h4 - p_left;
  283. sz_left -= adv;
  284. p_left += adv;
  285. bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
  286. sz_h4, mtk_recv_pkts,
  287. ARRAY_SIZE(mtk_recv_pkts));
  288. if (IS_ERR(bdev->rx_skb)) {
  289. err = PTR_ERR(bdev->rx_skb);
  290. bt_dev_err(bdev->hdev,
  291. "Frame reassembly failed (%d)", err);
  292. bdev->rx_skb = NULL;
  293. return err;
  294. }
  295. sz_left -= sz_h4;
  296. p_left += sz_h4;
  297. }
  298. return 0;
  299. }
  300. static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
  301. size_t count)
  302. {
  303. struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
  304. int err;
  305. err = btmtkuart_recv(bdev->hdev, data, count);
  306. if (err < 0)
  307. return err;
  308. bdev->hdev->stat.byte_rx += count;
  309. return count;
  310. }
  311. static void btmtkuart_write_wakeup(struct serdev_device *serdev)
  312. {
  313. struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
  314. btmtkuart_tx_wakeup(bdev);
  315. }
  316. static const struct serdev_device_ops btmtkuart_client_ops = {
  317. .receive_buf = btmtkuart_receive_buf,
  318. .write_wakeup = btmtkuart_write_wakeup,
  319. };
  320. static int btmtkuart_open(struct hci_dev *hdev)
  321. {
  322. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  323. struct device *dev;
  324. int err;
  325. err = serdev_device_open(bdev->serdev);
  326. if (err) {
  327. bt_dev_err(hdev, "Unable to open UART device %s",
  328. dev_name(&bdev->serdev->dev));
  329. goto err_open;
  330. }
  331. bdev->stp_cursor = 2;
  332. bdev->stp_dlen = 0;
  333. dev = &bdev->serdev->dev;
  334. /* Enable the power domain and clock the device requires */
  335. pm_runtime_enable(dev);
  336. err = pm_runtime_get_sync(dev);
  337. if (err < 0) {
  338. pm_runtime_put_noidle(dev);
  339. goto err_disable_rpm;
  340. }
  341. err = clk_prepare_enable(bdev->clk);
  342. if (err < 0)
  343. goto err_put_rpm;
  344. return 0;
  345. err_put_rpm:
  346. pm_runtime_put_sync(dev);
  347. err_disable_rpm:
  348. pm_runtime_disable(dev);
  349. err_open:
  350. return err;
  351. }
  352. static int btmtkuart_close(struct hci_dev *hdev)
  353. {
  354. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  355. struct device *dev = &bdev->serdev->dev;
  356. /* Shutdown the clock and power domain the device requires */
  357. clk_disable_unprepare(bdev->clk);
  358. pm_runtime_put_sync(dev);
  359. pm_runtime_disable(dev);
  360. serdev_device_close(bdev->serdev);
  361. return 0;
  362. }
  363. static int btmtkuart_flush(struct hci_dev *hdev)
  364. {
  365. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  366. /* Flush any pending characters */
  367. serdev_device_write_flush(bdev->serdev);
  368. skb_queue_purge(&bdev->txq);
  369. cancel_work_sync(&bdev->tx_work);
  370. kfree_skb(bdev->rx_skb);
  371. bdev->rx_skb = NULL;
  372. bdev->stp_cursor = 2;
  373. bdev->stp_dlen = 0;
  374. return 0;
  375. }
  376. static int btmtkuart_setup(struct hci_dev *hdev)
  377. {
  378. u8 param = 0x1;
  379. int err = 0;
  380. /* Setup a firmware which the device definitely requires */
  381. err = mtk_setup_fw(hdev);
  382. if (err < 0)
  383. return err;
  384. /* Activate function the firmware providing to */
  385. err = mtk_hci_wmt_sync(hdev, MTK_WMT_RST, 0x4, 0, 0);
  386. if (err < 0) {
  387. bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
  388. return err;
  389. }
  390. /* Enable Bluetooth protocol */
  391. err = mtk_hci_wmt_sync(hdev, MTK_WMT_FUNC_CTRL, 0x0, sizeof(param),
  392. &param);
  393. if (err < 0) {
  394. bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
  395. return err;
  396. }
  397. return 0;
  398. }
  399. static int btmtkuart_shutdown(struct hci_dev *hdev)
  400. {
  401. u8 param = 0x0;
  402. int err;
  403. /* Disable the device */
  404. err = mtk_hci_wmt_sync(hdev, MTK_WMT_FUNC_CTRL, 0x0, sizeof(param),
  405. &param);
  406. if (err < 0) {
  407. bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
  408. return err;
  409. }
  410. return 0;
  411. }
  412. static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  413. {
  414. struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
  415. struct mtk_stp_hdr *shdr;
  416. int err, dlen, type = 0;
  417. /* Prepend skb with frame type */
  418. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  419. /* Make sure that there is enough rooms for STP header and trailer */
  420. if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
  421. (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
  422. err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
  423. GFP_ATOMIC);
  424. if (err < 0)
  425. return err;
  426. }
  427. /* Add the STP header */
  428. dlen = skb->len;
  429. shdr = skb_push(skb, sizeof(*shdr));
  430. shdr->prefix = 0x80;
  431. shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
  432. shdr->cs = 0; /* MT7622 doesn't care about checksum value */
  433. /* Add the STP trailer */
  434. skb_put_zero(skb, MTK_STP_TLR_SIZE);
  435. skb_queue_tail(&bdev->txq, skb);
  436. btmtkuart_tx_wakeup(bdev);
  437. return 0;
  438. }
  439. static int btmtkuart_probe(struct serdev_device *serdev)
  440. {
  441. struct btmtkuart_dev *bdev;
  442. struct hci_dev *hdev;
  443. bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
  444. if (!bdev)
  445. return -ENOMEM;
  446. bdev->clk = devm_clk_get(&serdev->dev, "ref");
  447. if (IS_ERR(bdev->clk))
  448. return PTR_ERR(bdev->clk);
  449. bdev->serdev = serdev;
  450. serdev_device_set_drvdata(serdev, bdev);
  451. serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
  452. INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
  453. skb_queue_head_init(&bdev->txq);
  454. /* Initialize and register HCI device */
  455. hdev = hci_alloc_dev();
  456. if (!hdev) {
  457. dev_err(&serdev->dev, "Can't allocate HCI device\n");
  458. return -ENOMEM;
  459. }
  460. bdev->hdev = hdev;
  461. hdev->bus = HCI_UART;
  462. hci_set_drvdata(hdev, bdev);
  463. hdev->open = btmtkuart_open;
  464. hdev->close = btmtkuart_close;
  465. hdev->flush = btmtkuart_flush;
  466. hdev->setup = btmtkuart_setup;
  467. hdev->shutdown = btmtkuart_shutdown;
  468. hdev->send = btmtkuart_send_frame;
  469. SET_HCIDEV_DEV(hdev, &serdev->dev);
  470. hdev->manufacturer = 70;
  471. set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
  472. if (hci_register_dev(hdev) < 0) {
  473. dev_err(&serdev->dev, "Can't register HCI device\n");
  474. hci_free_dev(hdev);
  475. return -ENODEV;
  476. }
  477. return 0;
  478. }
  479. static void btmtkuart_remove(struct serdev_device *serdev)
  480. {
  481. struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
  482. struct hci_dev *hdev = bdev->hdev;
  483. hci_unregister_dev(hdev);
  484. hci_free_dev(hdev);
  485. }
  486. #ifdef CONFIG_OF
  487. static const struct of_device_id mtk_of_match_table[] = {
  488. { .compatible = "mediatek,mt7622-bluetooth"},
  489. { }
  490. };
  491. MODULE_DEVICE_TABLE(of, mtk_of_match_table);
  492. #endif
  493. static struct serdev_device_driver btmtkuart_driver = {
  494. .probe = btmtkuart_probe,
  495. .remove = btmtkuart_remove,
  496. .driver = {
  497. .name = "btmtkuart",
  498. .of_match_table = of_match_ptr(mtk_of_match_table),
  499. },
  500. };
  501. module_serdev_device_driver(btmtkuart_driver);
  502. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  503. MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
  504. MODULE_VERSION(VERSION);
  505. MODULE_LICENSE("GPL");
  506. MODULE_FIRMWARE(FIRMWARE_MT7622);