hci_ll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Texas Instruments' Bluetooth HCILL UART protocol
  4. *
  5. * HCILL (HCI Low Level) is a Texas Instruments' power management
  6. * protocol extension to H4.
  7. *
  8. * Copyright (C) 2007 Texas Instruments, Inc.
  9. *
  10. * Written by Ohad Ben-Cohen <ohad@bencohen.org>
  11. *
  12. * Acknowledgements:
  13. * This file is based on hci_h4.c, which was written
  14. * by Maxim Krasnyansky and Marcel Holtmann.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/types.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/firmware.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/errno.h>
  28. #include <linux/string.h>
  29. #include <linux/signal.h>
  30. #include <linux/ioctl.h>
  31. #include <linux/of.h>
  32. #include <linux/serdev.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/ti_wilink_st.h>
  35. #include <linux/clk.h>
  36. #include <net/bluetooth/bluetooth.h>
  37. #include <net/bluetooth/hci_core.h>
  38. #include <linux/gpio/consumer.h>
  39. #include <linux/nvmem-consumer.h>
  40. #include "hci_uart.h"
  41. /* Vendor-specific HCI commands */
  42. #define HCI_VS_WRITE_BD_ADDR 0xfc06
  43. #define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
  44. /* HCILL commands */
  45. #define HCILL_GO_TO_SLEEP_IND 0x30
  46. #define HCILL_GO_TO_SLEEP_ACK 0x31
  47. #define HCILL_WAKE_UP_IND 0x32
  48. #define HCILL_WAKE_UP_ACK 0x33
  49. /* HCILL states */
  50. enum hcill_states_e {
  51. HCILL_ASLEEP,
  52. HCILL_ASLEEP_TO_AWAKE,
  53. HCILL_AWAKE,
  54. HCILL_AWAKE_TO_ASLEEP
  55. };
  56. struct ll_device {
  57. struct hci_uart hu;
  58. struct serdev_device *serdev;
  59. struct gpio_desc *enable_gpio;
  60. struct clk *ext_clk;
  61. bdaddr_t bdaddr;
  62. };
  63. struct ll_struct {
  64. struct sk_buff *rx_skb;
  65. struct sk_buff_head txq;
  66. spinlock_t hcill_lock; /* HCILL state lock */
  67. unsigned long hcill_state; /* HCILL power state */
  68. struct sk_buff_head tx_wait_q; /* HCILL wait queue */
  69. };
  70. /*
  71. * Builds and sends an HCILL command packet.
  72. * These are very simple packets with only 1 cmd byte
  73. */
  74. static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
  75. {
  76. int err = 0;
  77. struct sk_buff *skb = NULL;
  78. struct ll_struct *ll = hu->priv;
  79. BT_DBG("hu %p cmd 0x%x", hu, cmd);
  80. /* allocate packet */
  81. skb = bt_skb_alloc(1, GFP_ATOMIC);
  82. if (!skb) {
  83. BT_ERR("cannot allocate memory for HCILL packet");
  84. err = -ENOMEM;
  85. goto out;
  86. }
  87. /* prepare packet */
  88. skb_put_u8(skb, cmd);
  89. /* send packet */
  90. skb_queue_tail(&ll->txq, skb);
  91. out:
  92. return err;
  93. }
  94. /* Initialize protocol */
  95. static int ll_open(struct hci_uart *hu)
  96. {
  97. struct ll_struct *ll;
  98. BT_DBG("hu %p", hu);
  99. ll = kzalloc(sizeof(*ll), GFP_KERNEL);
  100. if (!ll)
  101. return -ENOMEM;
  102. skb_queue_head_init(&ll->txq);
  103. skb_queue_head_init(&ll->tx_wait_q);
  104. spin_lock_init(&ll->hcill_lock);
  105. ll->hcill_state = HCILL_AWAKE;
  106. hu->priv = ll;
  107. if (hu->serdev) {
  108. struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
  109. if (!IS_ERR(lldev->ext_clk))
  110. clk_prepare_enable(lldev->ext_clk);
  111. }
  112. return 0;
  113. }
  114. /* Flush protocol data */
  115. static int ll_flush(struct hci_uart *hu)
  116. {
  117. struct ll_struct *ll = hu->priv;
  118. BT_DBG("hu %p", hu);
  119. skb_queue_purge(&ll->tx_wait_q);
  120. skb_queue_purge(&ll->txq);
  121. return 0;
  122. }
  123. /* Close protocol */
  124. static int ll_close(struct hci_uart *hu)
  125. {
  126. struct ll_struct *ll = hu->priv;
  127. BT_DBG("hu %p", hu);
  128. skb_queue_purge(&ll->tx_wait_q);
  129. skb_queue_purge(&ll->txq);
  130. kfree_skb(ll->rx_skb);
  131. if (hu->serdev) {
  132. struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
  133. gpiod_set_value_cansleep(lldev->enable_gpio, 0);
  134. clk_disable_unprepare(lldev->ext_clk);
  135. }
  136. hu->priv = NULL;
  137. kfree(ll);
  138. return 0;
  139. }
  140. /*
  141. * internal function, which does common work of the device wake up process:
  142. * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
  143. * 2. changes internal state to HCILL_AWAKE.
  144. * Note: assumes that hcill_lock spinlock is taken,
  145. * shouldn't be called otherwise!
  146. */
  147. static void __ll_do_awake(struct ll_struct *ll)
  148. {
  149. struct sk_buff *skb = NULL;
  150. while ((skb = skb_dequeue(&ll->tx_wait_q)))
  151. skb_queue_tail(&ll->txq, skb);
  152. ll->hcill_state = HCILL_AWAKE;
  153. }
  154. /*
  155. * Called upon a wake-up-indication from the device
  156. */
  157. static void ll_device_want_to_wakeup(struct hci_uart *hu)
  158. {
  159. unsigned long flags;
  160. struct ll_struct *ll = hu->priv;
  161. BT_DBG("hu %p", hu);
  162. /* lock hcill state */
  163. spin_lock_irqsave(&ll->hcill_lock, flags);
  164. switch (ll->hcill_state) {
  165. case HCILL_ASLEEP_TO_AWAKE:
  166. /*
  167. * This state means that both the host and the BRF chip
  168. * have simultaneously sent a wake-up-indication packet.
  169. * Traditionally, in this case, receiving a wake-up-indication
  170. * was enough and an additional wake-up-ack wasn't needed.
  171. * This has changed with the BRF6350, which does require an
  172. * explicit wake-up-ack. Other BRF versions, which do not
  173. * require an explicit ack here, do accept it, thus it is
  174. * perfectly safe to always send one.
  175. */
  176. BT_DBG("dual wake-up-indication");
  177. fallthrough;
  178. case HCILL_ASLEEP:
  179. /* acknowledge device wake up */
  180. if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
  181. BT_ERR("cannot acknowledge device wake up");
  182. goto out;
  183. }
  184. break;
  185. default:
  186. /* any other state is illegal */
  187. BT_ERR("received HCILL_WAKE_UP_IND in state %ld",
  188. ll->hcill_state);
  189. break;
  190. }
  191. /* send pending packets and change state to HCILL_AWAKE */
  192. __ll_do_awake(ll);
  193. out:
  194. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  195. /* actually send the packets */
  196. hci_uart_tx_wakeup(hu);
  197. }
  198. /*
  199. * Called upon a sleep-indication from the device
  200. */
  201. static void ll_device_want_to_sleep(struct hci_uart *hu)
  202. {
  203. unsigned long flags;
  204. struct ll_struct *ll = hu->priv;
  205. BT_DBG("hu %p", hu);
  206. /* lock hcill state */
  207. spin_lock_irqsave(&ll->hcill_lock, flags);
  208. /* sanity check */
  209. if (ll->hcill_state != HCILL_AWAKE)
  210. BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld",
  211. ll->hcill_state);
  212. /* acknowledge device sleep */
  213. if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
  214. BT_ERR("cannot acknowledge device sleep");
  215. goto out;
  216. }
  217. /* update state */
  218. ll->hcill_state = HCILL_ASLEEP;
  219. out:
  220. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  221. /* actually send the sleep ack packet */
  222. hci_uart_tx_wakeup(hu);
  223. }
  224. /*
  225. * Called upon wake-up-acknowledgement from the device
  226. */
  227. static void ll_device_woke_up(struct hci_uart *hu)
  228. {
  229. unsigned long flags;
  230. struct ll_struct *ll = hu->priv;
  231. BT_DBG("hu %p", hu);
  232. /* lock hcill state */
  233. spin_lock_irqsave(&ll->hcill_lock, flags);
  234. /* sanity check */
  235. if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
  236. BT_ERR("received HCILL_WAKE_UP_ACK in state %ld",
  237. ll->hcill_state);
  238. /* send pending packets and change state to HCILL_AWAKE */
  239. __ll_do_awake(ll);
  240. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  241. /* actually send the packets */
  242. hci_uart_tx_wakeup(hu);
  243. }
  244. /* Enqueue frame for transmittion (padding, crc, etc) */
  245. /* may be called from two simultaneous tasklets */
  246. static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  247. {
  248. unsigned long flags = 0;
  249. struct ll_struct *ll = hu->priv;
  250. BT_DBG("hu %p skb %p", hu, skb);
  251. /* Prepend skb with frame type */
  252. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  253. /* lock hcill state */
  254. spin_lock_irqsave(&ll->hcill_lock, flags);
  255. /* act according to current state */
  256. switch (ll->hcill_state) {
  257. case HCILL_AWAKE:
  258. BT_DBG("device awake, sending normally");
  259. skb_queue_tail(&ll->txq, skb);
  260. break;
  261. case HCILL_ASLEEP:
  262. BT_DBG("device asleep, waking up and queueing packet");
  263. /* save packet for later */
  264. skb_queue_tail(&ll->tx_wait_q, skb);
  265. /* awake device */
  266. if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
  267. BT_ERR("cannot wake up device");
  268. break;
  269. }
  270. ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
  271. break;
  272. case HCILL_ASLEEP_TO_AWAKE:
  273. BT_DBG("device waking up, queueing packet");
  274. /* transient state; just keep packet for later */
  275. skb_queue_tail(&ll->tx_wait_q, skb);
  276. break;
  277. default:
  278. BT_ERR("illegal hcill state: %ld (losing packet)",
  279. ll->hcill_state);
  280. kfree_skb(skb);
  281. break;
  282. }
  283. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  284. return 0;
  285. }
  286. static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
  287. {
  288. struct hci_uart *hu = hci_get_drvdata(hdev);
  289. struct ll_struct *ll = hu->priv;
  290. switch (hci_skb_pkt_type(skb)) {
  291. case HCILL_GO_TO_SLEEP_IND:
  292. BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
  293. ll_device_want_to_sleep(hu);
  294. break;
  295. case HCILL_GO_TO_SLEEP_ACK:
  296. /* shouldn't happen */
  297. bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
  298. ll->hcill_state);
  299. break;
  300. case HCILL_WAKE_UP_IND:
  301. BT_DBG("HCILL_WAKE_UP_IND packet");
  302. ll_device_want_to_wakeup(hu);
  303. break;
  304. case HCILL_WAKE_UP_ACK:
  305. BT_DBG("HCILL_WAKE_UP_ACK packet");
  306. ll_device_woke_up(hu);
  307. break;
  308. }
  309. kfree_skb(skb);
  310. return 0;
  311. }
  312. #define LL_RECV_SLEEP_IND \
  313. .type = HCILL_GO_TO_SLEEP_IND, \
  314. .hlen = 0, \
  315. .loff = 0, \
  316. .lsize = 0, \
  317. .maxlen = 0
  318. #define LL_RECV_SLEEP_ACK \
  319. .type = HCILL_GO_TO_SLEEP_ACK, \
  320. .hlen = 0, \
  321. .loff = 0, \
  322. .lsize = 0, \
  323. .maxlen = 0
  324. #define LL_RECV_WAKE_IND \
  325. .type = HCILL_WAKE_UP_IND, \
  326. .hlen = 0, \
  327. .loff = 0, \
  328. .lsize = 0, \
  329. .maxlen = 0
  330. #define LL_RECV_WAKE_ACK \
  331. .type = HCILL_WAKE_UP_ACK, \
  332. .hlen = 0, \
  333. .loff = 0, \
  334. .lsize = 0, \
  335. .maxlen = 0
  336. static const struct h4_recv_pkt ll_recv_pkts[] = {
  337. { H4_RECV_ACL, .recv = hci_recv_frame },
  338. { H4_RECV_SCO, .recv = hci_recv_frame },
  339. { H4_RECV_EVENT, .recv = hci_recv_frame },
  340. { LL_RECV_SLEEP_IND, .recv = ll_recv_frame },
  341. { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame },
  342. { LL_RECV_WAKE_IND, .recv = ll_recv_frame },
  343. { LL_RECV_WAKE_ACK, .recv = ll_recv_frame },
  344. };
  345. /* Recv data */
  346. static int ll_recv(struct hci_uart *hu, const void *data, int count)
  347. {
  348. struct ll_struct *ll = hu->priv;
  349. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  350. return -EUNATCH;
  351. ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
  352. ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
  353. if (IS_ERR(ll->rx_skb)) {
  354. int err = PTR_ERR(ll->rx_skb);
  355. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  356. ll->rx_skb = NULL;
  357. return err;
  358. }
  359. return count;
  360. }
  361. static struct sk_buff *ll_dequeue(struct hci_uart *hu)
  362. {
  363. struct ll_struct *ll = hu->priv;
  364. return skb_dequeue(&ll->txq);
  365. }
  366. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  367. static int read_local_version(struct hci_dev *hdev)
  368. {
  369. int err = 0;
  370. unsigned short version = 0;
  371. struct sk_buff *skb;
  372. struct hci_rp_read_local_version *ver;
  373. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  374. HCI_INIT_TIMEOUT);
  375. if (IS_ERR(skb)) {
  376. bt_dev_err(hdev, "Reading TI version information failed (%ld)",
  377. PTR_ERR(skb));
  378. return PTR_ERR(skb);
  379. }
  380. if (skb->len != sizeof(*ver)) {
  381. err = -EILSEQ;
  382. goto out;
  383. }
  384. ver = (struct hci_rp_read_local_version *)skb->data;
  385. if (le16_to_cpu(ver->manufacturer) != 13) {
  386. err = -ENODEV;
  387. goto out;
  388. }
  389. version = le16_to_cpu(ver->lmp_subver);
  390. out:
  391. if (err)
  392. bt_dev_err(hdev, "Failed to read TI version info: %d", err);
  393. kfree_skb(skb);
  394. return err ? err : version;
  395. }
  396. static int send_command_from_firmware(struct ll_device *lldev,
  397. struct hci_command *cmd)
  398. {
  399. struct sk_buff *skb;
  400. if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
  401. /* ignore remote change
  402. * baud rate HCI VS command
  403. */
  404. bt_dev_warn(lldev->hu.hdev,
  405. "change remote baud rate command in firmware");
  406. return 0;
  407. }
  408. if (cmd->prefix != 1)
  409. bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
  410. skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen,
  411. &cmd->speed, HCI_INIT_TIMEOUT);
  412. if (IS_ERR(skb)) {
  413. bt_dev_err(lldev->hu.hdev, "send command failed");
  414. return PTR_ERR(skb);
  415. }
  416. kfree_skb(skb);
  417. return 0;
  418. }
  419. /**
  420. * download_firmware -
  421. * internal function which parses through the .bts firmware
  422. * script file intreprets SEND, DELAY actions only as of now
  423. */
  424. static int download_firmware(struct ll_device *lldev)
  425. {
  426. unsigned short chip, min_ver, maj_ver;
  427. int version, err, len;
  428. unsigned char *ptr, *action_ptr;
  429. unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
  430. const struct firmware *fw;
  431. struct hci_command *cmd;
  432. version = read_local_version(lldev->hu.hdev);
  433. if (version < 0)
  434. return version;
  435. chip = (version & 0x7C00) >> 10;
  436. min_ver = (version & 0x007F);
  437. maj_ver = (version & 0x0380) >> 7;
  438. if (version & 0x8000)
  439. maj_ver |= 0x0008;
  440. snprintf(bts_scr_name, sizeof(bts_scr_name),
  441. "ti-connectivity/TIInit_%d.%d.%d.bts",
  442. chip, maj_ver, min_ver);
  443. err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
  444. if (err || !fw->data || !fw->size) {
  445. bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
  446. err, bts_scr_name);
  447. return -EINVAL;
  448. }
  449. ptr = (void *)fw->data;
  450. len = fw->size;
  451. /* bts_header to remove out magic number and
  452. * version
  453. */
  454. ptr += sizeof(struct bts_header);
  455. len -= sizeof(struct bts_header);
  456. while (len > 0 && ptr) {
  457. bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
  458. ((struct bts_action *)ptr)->size,
  459. ((struct bts_action *)ptr)->type);
  460. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  461. switch (((struct bts_action *)ptr)->type) {
  462. case ACTION_SEND_COMMAND: /* action send */
  463. bt_dev_dbg(lldev->hu.hdev, "S");
  464. cmd = (struct hci_command *)action_ptr;
  465. err = send_command_from_firmware(lldev, cmd);
  466. if (err)
  467. goto out_rel_fw;
  468. break;
  469. case ACTION_WAIT_EVENT: /* wait */
  470. /* no need to wait as command was synchronous */
  471. bt_dev_dbg(lldev->hu.hdev, "W");
  472. break;
  473. case ACTION_DELAY: /* sleep */
  474. bt_dev_info(lldev->hu.hdev, "sleep command in scr");
  475. msleep(((struct bts_action_delay *)action_ptr)->msec);
  476. break;
  477. }
  478. len -= (sizeof(struct bts_action) +
  479. ((struct bts_action *)ptr)->size);
  480. ptr += sizeof(struct bts_action) +
  481. ((struct bts_action *)ptr)->size;
  482. }
  483. out_rel_fw:
  484. /* fw download complete */
  485. release_firmware(fw);
  486. return err;
  487. }
  488. static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  489. {
  490. bdaddr_t bdaddr_swapped;
  491. struct sk_buff *skb;
  492. /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
  493. * address to be MSB first, but bdaddr_t has the convention of being
  494. * LSB first.
  495. */
  496. baswap(&bdaddr_swapped, bdaddr);
  497. skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
  498. &bdaddr_swapped, HCI_INIT_TIMEOUT);
  499. if (!IS_ERR(skb))
  500. kfree_skb(skb);
  501. return PTR_ERR_OR_ZERO(skb);
  502. }
  503. static int ll_setup(struct hci_uart *hu)
  504. {
  505. int err, retry = 3;
  506. struct ll_device *lldev;
  507. struct serdev_device *serdev = hu->serdev;
  508. u32 speed;
  509. if (!serdev)
  510. return 0;
  511. lldev = serdev_device_get_drvdata(serdev);
  512. hu->hdev->set_bdaddr = ll_set_bdaddr;
  513. serdev_device_set_flow_control(serdev, true);
  514. do {
  515. /* Reset the Bluetooth device */
  516. gpiod_set_value_cansleep(lldev->enable_gpio, 0);
  517. msleep(5);
  518. gpiod_set_value_cansleep(lldev->enable_gpio, 1);
  519. err = serdev_device_wait_for_cts(serdev, true, 200);
  520. if (err) {
  521. bt_dev_err(hu->hdev, "Failed to get CTS");
  522. return err;
  523. }
  524. err = download_firmware(lldev);
  525. if (!err)
  526. break;
  527. /* Toggle BT_EN and retry */
  528. bt_dev_err(hu->hdev, "download firmware failed, retrying...");
  529. } while (retry--);
  530. if (err)
  531. return err;
  532. /* Set BD address if one was specified at probe */
  533. if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
  534. /* This means that there was an error getting the BD address
  535. * during probe, so mark the device as having a bad address.
  536. */
  537. set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
  538. } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
  539. err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
  540. if (err)
  541. set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
  542. }
  543. /* Operational speed if any */
  544. if (hu->oper_speed)
  545. speed = hu->oper_speed;
  546. else if (hu->proto->oper_speed)
  547. speed = hu->proto->oper_speed;
  548. else
  549. speed = 0;
  550. if (speed) {
  551. __le32 speed_le = cpu_to_le32(speed);
  552. struct sk_buff *skb;
  553. skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
  554. sizeof(speed_le), &speed_le,
  555. HCI_INIT_TIMEOUT);
  556. if (!IS_ERR(skb)) {
  557. kfree_skb(skb);
  558. serdev_device_set_baudrate(serdev, speed);
  559. }
  560. }
  561. return 0;
  562. }
  563. static const struct hci_uart_proto llp;
  564. static int hci_ti_probe(struct serdev_device *serdev)
  565. {
  566. struct hci_uart *hu;
  567. struct ll_device *lldev;
  568. struct nvmem_cell *bdaddr_cell;
  569. u32 max_speed = 3000000;
  570. lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
  571. if (!lldev)
  572. return -ENOMEM;
  573. hu = &lldev->hu;
  574. serdev_device_set_drvdata(serdev, lldev);
  575. lldev->serdev = hu->serdev = serdev;
  576. lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev,
  577. "enable",
  578. GPIOD_OUT_LOW);
  579. if (IS_ERR(lldev->enable_gpio))
  580. return PTR_ERR(lldev->enable_gpio);
  581. lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
  582. if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
  583. return PTR_ERR(lldev->ext_clk);
  584. of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
  585. hci_uart_set_speeds(hu, 115200, max_speed);
  586. /* optional BD address from nvram */
  587. bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
  588. if (IS_ERR(bdaddr_cell)) {
  589. int err = PTR_ERR(bdaddr_cell);
  590. if (err == -EPROBE_DEFER)
  591. return err;
  592. /* ENOENT means there is no matching nvmem cell and ENOSYS
  593. * means that nvmem is not enabled in the kernel configuration.
  594. */
  595. if (err != -ENOENT && err != -ENOSYS) {
  596. /* If there was some other error, give userspace a
  597. * chance to fix the problem instead of failing to load
  598. * the driver. Using BDADDR_NONE as a flag that is
  599. * tested later in the setup function.
  600. */
  601. dev_warn(&serdev->dev,
  602. "Failed to get \"bd-address\" nvmem cell (%d)\n",
  603. err);
  604. bacpy(&lldev->bdaddr, BDADDR_NONE);
  605. }
  606. } else {
  607. bdaddr_t *bdaddr;
  608. size_t len;
  609. bdaddr = nvmem_cell_read(bdaddr_cell, &len);
  610. nvmem_cell_put(bdaddr_cell);
  611. if (IS_ERR(bdaddr)) {
  612. dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
  613. return PTR_ERR(bdaddr);
  614. }
  615. if (len != sizeof(bdaddr_t)) {
  616. dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
  617. kfree(bdaddr);
  618. return -EINVAL;
  619. }
  620. /* As per the device tree bindings, the value from nvmem is
  621. * expected to be MSB first, but in the kernel it is expected
  622. * that bdaddr_t is LSB first.
  623. */
  624. baswap(&lldev->bdaddr, bdaddr);
  625. kfree(bdaddr);
  626. }
  627. return hci_uart_register_device(hu, &llp);
  628. }
  629. static void hci_ti_remove(struct serdev_device *serdev)
  630. {
  631. struct ll_device *lldev = serdev_device_get_drvdata(serdev);
  632. hci_uart_unregister_device(&lldev->hu);
  633. }
  634. static const struct of_device_id hci_ti_of_match[] = {
  635. { .compatible = "ti,cc2560" },
  636. { .compatible = "ti,wl1271-st" },
  637. { .compatible = "ti,wl1273-st" },
  638. { .compatible = "ti,wl1281-st" },
  639. { .compatible = "ti,wl1283-st" },
  640. { .compatible = "ti,wl1285-st" },
  641. { .compatible = "ti,wl1801-st" },
  642. { .compatible = "ti,wl1805-st" },
  643. { .compatible = "ti,wl1807-st" },
  644. { .compatible = "ti,wl1831-st" },
  645. { .compatible = "ti,wl1835-st" },
  646. { .compatible = "ti,wl1837-st" },
  647. {},
  648. };
  649. MODULE_DEVICE_TABLE(of, hci_ti_of_match);
  650. static struct serdev_device_driver hci_ti_drv = {
  651. .driver = {
  652. .name = "hci-ti",
  653. .of_match_table = of_match_ptr(hci_ti_of_match),
  654. },
  655. .probe = hci_ti_probe,
  656. .remove = hci_ti_remove,
  657. };
  658. #else
  659. #define ll_setup NULL
  660. #endif
  661. static const struct hci_uart_proto llp = {
  662. .id = HCI_UART_LL,
  663. .name = "LL",
  664. .setup = ll_setup,
  665. .open = ll_open,
  666. .close = ll_close,
  667. .recv = ll_recv,
  668. .enqueue = ll_enqueue,
  669. .dequeue = ll_dequeue,
  670. .flush = ll_flush,
  671. };
  672. int __init ll_init(void)
  673. {
  674. serdev_device_driver_register(&hci_ti_drv);
  675. return hci_uart_register_proto(&llp);
  676. }
  677. int __exit ll_deinit(void)
  678. {
  679. serdev_device_driver_unregister(&hci_ti_drv);
  680. return hci_uart_unregister_proto(&llp);
  681. }