hci_ll.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. serdev_device_open(hu->serdev);
  123. if (!IS_ERR(lldev->ext_clk))
  124. clk_prepare_enable(lldev->ext_clk);
  125. }
  126. return 0;
  127. }
  128. /* Flush protocol data */
  129. static int ll_flush(struct hci_uart *hu)
  130. {
  131. struct ll_struct *ll = hu->priv;
  132. BT_DBG("hu %p", hu);
  133. skb_queue_purge(&ll->tx_wait_q);
  134. skb_queue_purge(&ll->txq);
  135. return 0;
  136. }
  137. /* Close protocol */
  138. static int ll_close(struct hci_uart *hu)
  139. {
  140. struct ll_struct *ll = hu->priv;
  141. BT_DBG("hu %p", hu);
  142. skb_queue_purge(&ll->tx_wait_q);
  143. skb_queue_purge(&ll->txq);
  144. kfree_skb(ll->rx_skb);
  145. if (hu->serdev) {
  146. struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
  147. gpiod_set_value_cansleep(lldev->enable_gpio, 0);
  148. clk_disable_unprepare(lldev->ext_clk);
  149. serdev_device_close(hu->serdev);
  150. }
  151. hu->priv = NULL;
  152. kfree(ll);
  153. return 0;
  154. }
  155. /*
  156. * internal function, which does common work of the device wake up process:
  157. * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
  158. * 2. changes internal state to HCILL_AWAKE.
  159. * Note: assumes that hcill_lock spinlock is taken,
  160. * shouldn't be called otherwise!
  161. */
  162. static void __ll_do_awake(struct ll_struct *ll)
  163. {
  164. struct sk_buff *skb = NULL;
  165. while ((skb = skb_dequeue(&ll->tx_wait_q)))
  166. skb_queue_tail(&ll->txq, skb);
  167. ll->hcill_state = HCILL_AWAKE;
  168. }
  169. /*
  170. * Called upon a wake-up-indication from the device
  171. */
  172. static void ll_device_want_to_wakeup(struct hci_uart *hu)
  173. {
  174. unsigned long flags;
  175. struct ll_struct *ll = hu->priv;
  176. BT_DBG("hu %p", hu);
  177. /* lock hcill state */
  178. spin_lock_irqsave(&ll->hcill_lock, flags);
  179. switch (ll->hcill_state) {
  180. case HCILL_ASLEEP_TO_AWAKE:
  181. /*
  182. * This state means that both the host and the BRF chip
  183. * have simultaneously sent a wake-up-indication packet.
  184. * Traditionally, in this case, receiving a wake-up-indication
  185. * was enough and an additional wake-up-ack wasn't needed.
  186. * This has changed with the BRF6350, which does require an
  187. * explicit wake-up-ack. Other BRF versions, which do not
  188. * require an explicit ack here, do accept it, thus it is
  189. * perfectly safe to always send one.
  190. */
  191. BT_DBG("dual wake-up-indication");
  192. /* fall through */
  193. case HCILL_ASLEEP:
  194. /* acknowledge device wake up */
  195. if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
  196. BT_ERR("cannot acknowledge device wake up");
  197. goto out;
  198. }
  199. break;
  200. default:
  201. /* any other state is illegal */
  202. BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
  203. break;
  204. }
  205. /* send pending packets and change state to HCILL_AWAKE */
  206. __ll_do_awake(ll);
  207. out:
  208. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  209. /* actually send the packets */
  210. hci_uart_tx_wakeup(hu);
  211. }
  212. /*
  213. * Called upon a sleep-indication from the device
  214. */
  215. static void ll_device_want_to_sleep(struct hci_uart *hu)
  216. {
  217. unsigned long flags;
  218. struct ll_struct *ll = hu->priv;
  219. BT_DBG("hu %p", hu);
  220. /* lock hcill state */
  221. spin_lock_irqsave(&ll->hcill_lock, flags);
  222. /* sanity check */
  223. if (ll->hcill_state != HCILL_AWAKE)
  224. BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
  225. /* acknowledge device sleep */
  226. if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
  227. BT_ERR("cannot acknowledge device sleep");
  228. goto out;
  229. }
  230. /* update state */
  231. ll->hcill_state = HCILL_ASLEEP;
  232. out:
  233. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  234. /* actually send the sleep ack packet */
  235. hci_uart_tx_wakeup(hu);
  236. }
  237. /*
  238. * Called upon wake-up-acknowledgement from the device
  239. */
  240. static void ll_device_woke_up(struct hci_uart *hu)
  241. {
  242. unsigned long flags;
  243. struct ll_struct *ll = hu->priv;
  244. BT_DBG("hu %p", hu);
  245. /* lock hcill state */
  246. spin_lock_irqsave(&ll->hcill_lock, flags);
  247. /* sanity check */
  248. if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
  249. BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
  250. /* send pending packets and change state to HCILL_AWAKE */
  251. __ll_do_awake(ll);
  252. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  253. /* actually send the packets */
  254. hci_uart_tx_wakeup(hu);
  255. }
  256. /* Enqueue frame for transmittion (padding, crc, etc) */
  257. /* may be called from two simultaneous tasklets */
  258. static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  259. {
  260. unsigned long flags = 0;
  261. struct ll_struct *ll = hu->priv;
  262. BT_DBG("hu %p skb %p", hu, skb);
  263. /* Prepend skb with frame type */
  264. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  265. /* lock hcill state */
  266. spin_lock_irqsave(&ll->hcill_lock, flags);
  267. /* act according to current state */
  268. switch (ll->hcill_state) {
  269. case HCILL_AWAKE:
  270. BT_DBG("device awake, sending normally");
  271. skb_queue_tail(&ll->txq, skb);
  272. break;
  273. case HCILL_ASLEEP:
  274. BT_DBG("device asleep, waking up and queueing packet");
  275. /* save packet for later */
  276. skb_queue_tail(&ll->tx_wait_q, skb);
  277. /* awake device */
  278. if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
  279. BT_ERR("cannot wake up device");
  280. break;
  281. }
  282. ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
  283. break;
  284. case HCILL_ASLEEP_TO_AWAKE:
  285. BT_DBG("device waking up, queueing packet");
  286. /* transient state; just keep packet for later */
  287. skb_queue_tail(&ll->tx_wait_q, skb);
  288. break;
  289. default:
  290. BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
  291. kfree_skb(skb);
  292. break;
  293. }
  294. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  295. return 0;
  296. }
  297. static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
  298. {
  299. struct hci_uart *hu = hci_get_drvdata(hdev);
  300. struct ll_struct *ll = hu->priv;
  301. switch (hci_skb_pkt_type(skb)) {
  302. case HCILL_GO_TO_SLEEP_IND:
  303. BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
  304. ll_device_want_to_sleep(hu);
  305. break;
  306. case HCILL_GO_TO_SLEEP_ACK:
  307. /* shouldn't happen */
  308. bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
  309. ll->hcill_state);
  310. break;
  311. case HCILL_WAKE_UP_IND:
  312. BT_DBG("HCILL_WAKE_UP_IND packet");
  313. ll_device_want_to_wakeup(hu);
  314. break;
  315. case HCILL_WAKE_UP_ACK:
  316. BT_DBG("HCILL_WAKE_UP_ACK packet");
  317. ll_device_woke_up(hu);
  318. break;
  319. }
  320. kfree_skb(skb);
  321. return 0;
  322. }
  323. #define LL_RECV_SLEEP_IND \
  324. .type = HCILL_GO_TO_SLEEP_IND, \
  325. .hlen = 0, \
  326. .loff = 0, \
  327. .lsize = 0, \
  328. .maxlen = 0
  329. #define LL_RECV_SLEEP_ACK \
  330. .type = HCILL_GO_TO_SLEEP_ACK, \
  331. .hlen = 0, \
  332. .loff = 0, \
  333. .lsize = 0, \
  334. .maxlen = 0
  335. #define LL_RECV_WAKE_IND \
  336. .type = HCILL_WAKE_UP_IND, \
  337. .hlen = 0, \
  338. .loff = 0, \
  339. .lsize = 0, \
  340. .maxlen = 0
  341. #define LL_RECV_WAKE_ACK \
  342. .type = HCILL_WAKE_UP_ACK, \
  343. .hlen = 0, \
  344. .loff = 0, \
  345. .lsize = 0, \
  346. .maxlen = 0
  347. static const struct h4_recv_pkt ll_recv_pkts[] = {
  348. { H4_RECV_ACL, .recv = hci_recv_frame },
  349. { H4_RECV_SCO, .recv = hci_recv_frame },
  350. { H4_RECV_EVENT, .recv = hci_recv_frame },
  351. { LL_RECV_SLEEP_IND, .recv = ll_recv_frame },
  352. { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame },
  353. { LL_RECV_WAKE_IND, .recv = ll_recv_frame },
  354. { LL_RECV_WAKE_ACK, .recv = ll_recv_frame },
  355. };
  356. /* Recv data */
  357. static int ll_recv(struct hci_uart *hu, const void *data, int count)
  358. {
  359. struct ll_struct *ll = hu->priv;
  360. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  361. return -EUNATCH;
  362. ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
  363. ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
  364. if (IS_ERR(ll->rx_skb)) {
  365. int err = PTR_ERR(ll->rx_skb);
  366. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  367. ll->rx_skb = NULL;
  368. return err;
  369. }
  370. return count;
  371. }
  372. static struct sk_buff *ll_dequeue(struct hci_uart *hu)
  373. {
  374. struct ll_struct *ll = hu->priv;
  375. return skb_dequeue(&ll->txq);
  376. }
  377. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  378. static int read_local_version(struct hci_dev *hdev)
  379. {
  380. int err = 0;
  381. unsigned short version = 0;
  382. struct sk_buff *skb;
  383. struct hci_rp_read_local_version *ver;
  384. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
  385. if (IS_ERR(skb)) {
  386. bt_dev_err(hdev, "Reading TI version information failed (%ld)",
  387. PTR_ERR(skb));
  388. return PTR_ERR(skb);
  389. }
  390. if (skb->len != sizeof(*ver)) {
  391. err = -EILSEQ;
  392. goto out;
  393. }
  394. ver = (struct hci_rp_read_local_version *)skb->data;
  395. if (le16_to_cpu(ver->manufacturer) != 13) {
  396. err = -ENODEV;
  397. goto out;
  398. }
  399. version = le16_to_cpu(ver->lmp_subver);
  400. out:
  401. if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
  402. kfree_skb(skb);
  403. return err ? err : version;
  404. }
  405. /**
  406. * download_firmware -
  407. * internal function which parses through the .bts firmware
  408. * script file intreprets SEND, DELAY actions only as of now
  409. */
  410. static int download_firmware(struct ll_device *lldev)
  411. {
  412. unsigned short chip, min_ver, maj_ver;
  413. int version, err, len;
  414. unsigned char *ptr, *action_ptr;
  415. unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
  416. const struct firmware *fw;
  417. struct sk_buff *skb;
  418. struct hci_command *cmd;
  419. version = read_local_version(lldev->hu.hdev);
  420. if (version < 0)
  421. return version;
  422. chip = (version & 0x7C00) >> 10;
  423. min_ver = (version & 0x007F);
  424. maj_ver = (version & 0x0380) >> 7;
  425. if (version & 0x8000)
  426. maj_ver |= 0x0008;
  427. snprintf(bts_scr_name, sizeof(bts_scr_name),
  428. "ti-connectivity/TIInit_%d.%d.%d.bts",
  429. chip, maj_ver, min_ver);
  430. err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
  431. if (err || !fw->data || !fw->size) {
  432. bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
  433. err, bts_scr_name);
  434. return -EINVAL;
  435. }
  436. ptr = (void *)fw->data;
  437. len = fw->size;
  438. /* bts_header to remove out magic number and
  439. * version
  440. */
  441. ptr += sizeof(struct bts_header);
  442. len -= sizeof(struct bts_header);
  443. while (len > 0 && ptr) {
  444. bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
  445. ((struct bts_action *)ptr)->size,
  446. ((struct bts_action *)ptr)->type);
  447. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  448. switch (((struct bts_action *)ptr)->type) {
  449. case ACTION_SEND_COMMAND: /* action send */
  450. bt_dev_dbg(lldev->hu.hdev, "S");
  451. cmd = (struct hci_command *)action_ptr;
  452. if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
  453. /* ignore remote change
  454. * baud rate HCI VS command
  455. */
  456. bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
  457. break;
  458. }
  459. if (cmd->prefix != 1)
  460. bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
  461. skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
  462. if (IS_ERR(skb)) {
  463. bt_dev_err(lldev->hu.hdev, "send command failed");
  464. err = PTR_ERR(skb);
  465. goto out_rel_fw;
  466. }
  467. kfree_skb(skb);
  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, "enable", GPIOD_OUT_LOW);
  577. if (IS_ERR(lldev->enable_gpio))
  578. return PTR_ERR(lldev->enable_gpio);
  579. lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
  580. if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
  581. return PTR_ERR(lldev->ext_clk);
  582. of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
  583. hci_uart_set_speeds(hu, 115200, max_speed);
  584. /* optional BD address from nvram */
  585. bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
  586. if (IS_ERR(bdaddr_cell)) {
  587. int err = PTR_ERR(bdaddr_cell);
  588. if (err == -EPROBE_DEFER)
  589. return err;
  590. /* ENOENT means there is no matching nvmem cell and ENOSYS
  591. * means that nvmem is not enabled in the kernel configuration.
  592. */
  593. if (err != -ENOENT && err != -ENOSYS) {
  594. /* If there was some other error, give userspace a
  595. * chance to fix the problem instead of failing to load
  596. * the driver. Using BDADDR_NONE as a flag that is
  597. * tested later in the setup function.
  598. */
  599. dev_warn(&serdev->dev,
  600. "Failed to get \"bd-address\" nvmem cell (%d)\n",
  601. err);
  602. bacpy(&lldev->bdaddr, BDADDR_NONE);
  603. }
  604. } else {
  605. bdaddr_t *bdaddr;
  606. size_t len;
  607. bdaddr = nvmem_cell_read(bdaddr_cell, &len);
  608. nvmem_cell_put(bdaddr_cell);
  609. if (IS_ERR(bdaddr)) {
  610. dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
  611. return PTR_ERR(bdaddr);
  612. }
  613. if (len != sizeof(bdaddr_t)) {
  614. dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
  615. kfree(bdaddr);
  616. return -EINVAL;
  617. }
  618. /* As per the device tree bindings, the value from nvmem is
  619. * expected to be MSB first, but in the kernel it is expected
  620. * that bdaddr_t is LSB first.
  621. */
  622. baswap(&lldev->bdaddr, bdaddr);
  623. kfree(bdaddr);
  624. }
  625. return hci_uart_register_device(hu, &llp);
  626. }
  627. static void hci_ti_remove(struct serdev_device *serdev)
  628. {
  629. struct ll_device *lldev = serdev_device_get_drvdata(serdev);
  630. hci_uart_unregister_device(&lldev->hu);
  631. }
  632. static const struct of_device_id hci_ti_of_match[] = {
  633. { .compatible = "ti,cc2560" },
  634. { .compatible = "ti,wl1271-st" },
  635. { .compatible = "ti,wl1273-st" },
  636. { .compatible = "ti,wl1281-st" },
  637. { .compatible = "ti,wl1283-st" },
  638. { .compatible = "ti,wl1285-st" },
  639. { .compatible = "ti,wl1801-st" },
  640. { .compatible = "ti,wl1805-st" },
  641. { .compatible = "ti,wl1807-st" },
  642. { .compatible = "ti,wl1831-st" },
  643. { .compatible = "ti,wl1835-st" },
  644. { .compatible = "ti,wl1837-st" },
  645. {},
  646. };
  647. MODULE_DEVICE_TABLE(of, hci_ti_of_match);
  648. static struct serdev_device_driver hci_ti_drv = {
  649. .driver = {
  650. .name = "hci-ti",
  651. .of_match_table = of_match_ptr(hci_ti_of_match),
  652. },
  653. .probe = hci_ti_probe,
  654. .remove = hci_ti_remove,
  655. };
  656. #else
  657. #define ll_setup NULL
  658. #endif
  659. static const struct hci_uart_proto llp = {
  660. .id = HCI_UART_LL,
  661. .name = "LL",
  662. .setup = ll_setup,
  663. .open = ll_open,
  664. .close = ll_close,
  665. .recv = ll_recv,
  666. .enqueue = ll_enqueue,
  667. .dequeue = ll_dequeue,
  668. .flush = ll_flush,
  669. };
  670. int __init ll_init(void)
  671. {
  672. serdev_device_driver_register(&hci_ti_drv);
  673. return hci_uart_register_proto(&llp);
  674. }
  675. int __exit ll_deinit(void)
  676. {
  677. serdev_device_driver_unregister(&hci_ti_drv);
  678. return hci_uart_unregister_proto(&llp);
  679. }