hci_ll.c 20 KB

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