hci_ll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 "hci_uart.h"
  53. /* HCILL commands */
  54. #define HCILL_GO_TO_SLEEP_IND 0x30
  55. #define HCILL_GO_TO_SLEEP_ACK 0x31
  56. #define HCILL_WAKE_UP_IND 0x32
  57. #define HCILL_WAKE_UP_ACK 0x33
  58. /* HCILL receiver States */
  59. #define HCILL_W4_PACKET_TYPE 0
  60. #define HCILL_W4_EVENT_HDR 1
  61. #define HCILL_W4_ACL_HDR 2
  62. #define HCILL_W4_SCO_HDR 3
  63. #define HCILL_W4_DATA 4
  64. /* HCILL states */
  65. enum hcill_states_e {
  66. HCILL_ASLEEP,
  67. HCILL_ASLEEP_TO_AWAKE,
  68. HCILL_AWAKE,
  69. HCILL_AWAKE_TO_ASLEEP
  70. };
  71. struct hcill_cmd {
  72. u8 cmd;
  73. } __packed;
  74. struct ll_device {
  75. struct hci_uart hu;
  76. struct serdev_device *serdev;
  77. struct gpio_desc *enable_gpio;
  78. struct clk *ext_clk;
  79. };
  80. struct ll_struct {
  81. unsigned long rx_state;
  82. unsigned long rx_count;
  83. struct sk_buff *rx_skb;
  84. struct sk_buff_head txq;
  85. spinlock_t hcill_lock; /* HCILL state lock */
  86. unsigned long hcill_state; /* HCILL power state */
  87. struct sk_buff_head tx_wait_q; /* HCILL wait queue */
  88. };
  89. /*
  90. * Builds and sends an HCILL command packet.
  91. * These are very simple packets with only 1 cmd byte
  92. */
  93. static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
  94. {
  95. int err = 0;
  96. struct sk_buff *skb = NULL;
  97. struct ll_struct *ll = hu->priv;
  98. struct hcill_cmd *hcill_packet;
  99. BT_DBG("hu %p cmd 0x%x", hu, cmd);
  100. /* allocate packet */
  101. skb = bt_skb_alloc(1, GFP_ATOMIC);
  102. if (!skb) {
  103. BT_ERR("cannot allocate memory for HCILL packet");
  104. err = -ENOMEM;
  105. goto out;
  106. }
  107. /* prepare packet */
  108. hcill_packet = skb_put(skb, 1);
  109. hcill_packet->cmd = cmd;
  110. /* send packet */
  111. skb_queue_tail(&ll->txq, skb);
  112. out:
  113. return err;
  114. }
  115. /* Initialize protocol */
  116. static int ll_open(struct hci_uart *hu)
  117. {
  118. struct ll_struct *ll;
  119. BT_DBG("hu %p", hu);
  120. ll = kzalloc(sizeof(*ll), GFP_KERNEL);
  121. if (!ll)
  122. return -ENOMEM;
  123. skb_queue_head_init(&ll->txq);
  124. skb_queue_head_init(&ll->tx_wait_q);
  125. spin_lock_init(&ll->hcill_lock);
  126. ll->hcill_state = HCILL_AWAKE;
  127. hu->priv = ll;
  128. if (hu->serdev) {
  129. struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
  130. serdev_device_open(hu->serdev);
  131. if (!IS_ERR(lldev->ext_clk))
  132. clk_prepare_enable(lldev->ext_clk);
  133. }
  134. return 0;
  135. }
  136. /* Flush protocol data */
  137. static int ll_flush(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. return 0;
  144. }
  145. /* Close protocol */
  146. static int ll_close(struct hci_uart *hu)
  147. {
  148. struct ll_struct *ll = hu->priv;
  149. BT_DBG("hu %p", hu);
  150. skb_queue_purge(&ll->tx_wait_q);
  151. skb_queue_purge(&ll->txq);
  152. kfree_skb(ll->rx_skb);
  153. if (hu->serdev) {
  154. struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
  155. gpiod_set_value_cansleep(lldev->enable_gpio, 0);
  156. clk_disable_unprepare(lldev->ext_clk);
  157. serdev_device_close(hu->serdev);
  158. }
  159. hu->priv = NULL;
  160. kfree(ll);
  161. return 0;
  162. }
  163. /*
  164. * internal function, which does common work of the device wake up process:
  165. * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
  166. * 2. changes internal state to HCILL_AWAKE.
  167. * Note: assumes that hcill_lock spinlock is taken,
  168. * shouldn't be called otherwise!
  169. */
  170. static void __ll_do_awake(struct ll_struct *ll)
  171. {
  172. struct sk_buff *skb = NULL;
  173. while ((skb = skb_dequeue(&ll->tx_wait_q)))
  174. skb_queue_tail(&ll->txq, skb);
  175. ll->hcill_state = HCILL_AWAKE;
  176. }
  177. /*
  178. * Called upon a wake-up-indication from the device
  179. */
  180. static void ll_device_want_to_wakeup(struct hci_uart *hu)
  181. {
  182. unsigned long flags;
  183. struct ll_struct *ll = hu->priv;
  184. BT_DBG("hu %p", hu);
  185. /* lock hcill state */
  186. spin_lock_irqsave(&ll->hcill_lock, flags);
  187. switch (ll->hcill_state) {
  188. case HCILL_ASLEEP_TO_AWAKE:
  189. /*
  190. * This state means that both the host and the BRF chip
  191. * have simultaneously sent a wake-up-indication packet.
  192. * Traditionally, in this case, receiving a wake-up-indication
  193. * was enough and an additional wake-up-ack wasn't needed.
  194. * This has changed with the BRF6350, which does require an
  195. * explicit wake-up-ack. Other BRF versions, which do not
  196. * require an explicit ack here, do accept it, thus it is
  197. * perfectly safe to always send one.
  198. */
  199. BT_DBG("dual wake-up-indication");
  200. /* deliberate fall-through - do not add break */
  201. case HCILL_ASLEEP:
  202. /* acknowledge device wake up */
  203. if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
  204. BT_ERR("cannot acknowledge device wake up");
  205. goto out;
  206. }
  207. break;
  208. default:
  209. /* any other state is illegal */
  210. BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
  211. break;
  212. }
  213. /* send pending packets and change state to HCILL_AWAKE */
  214. __ll_do_awake(ll);
  215. out:
  216. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  217. /* actually send the packets */
  218. hci_uart_tx_wakeup(hu);
  219. }
  220. /*
  221. * Called upon a sleep-indication from the device
  222. */
  223. static void ll_device_want_to_sleep(struct hci_uart *hu)
  224. {
  225. unsigned long flags;
  226. struct ll_struct *ll = hu->priv;
  227. BT_DBG("hu %p", hu);
  228. /* lock hcill state */
  229. spin_lock_irqsave(&ll->hcill_lock, flags);
  230. /* sanity check */
  231. if (ll->hcill_state != HCILL_AWAKE)
  232. BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
  233. /* acknowledge device sleep */
  234. if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
  235. BT_ERR("cannot acknowledge device sleep");
  236. goto out;
  237. }
  238. /* update state */
  239. ll->hcill_state = HCILL_ASLEEP;
  240. out:
  241. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  242. /* actually send the sleep ack packet */
  243. hci_uart_tx_wakeup(hu);
  244. }
  245. /*
  246. * Called upon wake-up-acknowledgement from the device
  247. */
  248. static void ll_device_woke_up(struct hci_uart *hu)
  249. {
  250. unsigned long flags;
  251. struct ll_struct *ll = hu->priv;
  252. BT_DBG("hu %p", hu);
  253. /* lock hcill state */
  254. spin_lock_irqsave(&ll->hcill_lock, flags);
  255. /* sanity check */
  256. if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
  257. BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
  258. /* send pending packets and change state to HCILL_AWAKE */
  259. __ll_do_awake(ll);
  260. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  261. /* actually send the packets */
  262. hci_uart_tx_wakeup(hu);
  263. }
  264. /* Enqueue frame for transmittion (padding, crc, etc) */
  265. /* may be called from two simultaneous tasklets */
  266. static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  267. {
  268. unsigned long flags = 0;
  269. struct ll_struct *ll = hu->priv;
  270. BT_DBG("hu %p skb %p", hu, skb);
  271. /* Prepend skb with frame type */
  272. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  273. /* lock hcill state */
  274. spin_lock_irqsave(&ll->hcill_lock, flags);
  275. /* act according to current state */
  276. switch (ll->hcill_state) {
  277. case HCILL_AWAKE:
  278. BT_DBG("device awake, sending normally");
  279. skb_queue_tail(&ll->txq, skb);
  280. break;
  281. case HCILL_ASLEEP:
  282. BT_DBG("device asleep, waking up and queueing packet");
  283. /* save packet for later */
  284. skb_queue_tail(&ll->tx_wait_q, skb);
  285. /* awake device */
  286. if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
  287. BT_ERR("cannot wake up device");
  288. break;
  289. }
  290. ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
  291. break;
  292. case HCILL_ASLEEP_TO_AWAKE:
  293. BT_DBG("device waking up, queueing packet");
  294. /* transient state; just keep packet for later */
  295. skb_queue_tail(&ll->tx_wait_q, skb);
  296. break;
  297. default:
  298. BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
  299. kfree_skb(skb);
  300. break;
  301. }
  302. spin_unlock_irqrestore(&ll->hcill_lock, flags);
  303. return 0;
  304. }
  305. static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
  306. {
  307. int room = skb_tailroom(ll->rx_skb);
  308. BT_DBG("len %d room %d", len, room);
  309. if (!len) {
  310. hci_recv_frame(hdev, ll->rx_skb);
  311. } else if (len > room) {
  312. BT_ERR("Data length is too large");
  313. kfree_skb(ll->rx_skb);
  314. } else {
  315. ll->rx_state = HCILL_W4_DATA;
  316. ll->rx_count = len;
  317. return len;
  318. }
  319. ll->rx_state = HCILL_W4_PACKET_TYPE;
  320. ll->rx_skb = NULL;
  321. ll->rx_count = 0;
  322. return 0;
  323. }
  324. /* Recv data */
  325. static int ll_recv(struct hci_uart *hu, const void *data, int count)
  326. {
  327. struct ll_struct *ll = hu->priv;
  328. const char *ptr;
  329. struct hci_event_hdr *eh;
  330. struct hci_acl_hdr *ah;
  331. struct hci_sco_hdr *sh;
  332. int len, type, dlen;
  333. BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
  334. ptr = data;
  335. while (count) {
  336. if (ll->rx_count) {
  337. len = min_t(unsigned int, ll->rx_count, count);
  338. skb_put_data(ll->rx_skb, ptr, len);
  339. ll->rx_count -= len; count -= len; ptr += len;
  340. if (ll->rx_count)
  341. continue;
  342. switch (ll->rx_state) {
  343. case HCILL_W4_DATA:
  344. BT_DBG("Complete data");
  345. hci_recv_frame(hu->hdev, ll->rx_skb);
  346. ll->rx_state = HCILL_W4_PACKET_TYPE;
  347. ll->rx_skb = NULL;
  348. continue;
  349. case HCILL_W4_EVENT_HDR:
  350. eh = hci_event_hdr(ll->rx_skb);
  351. BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
  352. ll_check_data_len(hu->hdev, ll, eh->plen);
  353. continue;
  354. case HCILL_W4_ACL_HDR:
  355. ah = hci_acl_hdr(ll->rx_skb);
  356. dlen = __le16_to_cpu(ah->dlen);
  357. BT_DBG("ACL header: dlen %d", dlen);
  358. ll_check_data_len(hu->hdev, ll, dlen);
  359. continue;
  360. case HCILL_W4_SCO_HDR:
  361. sh = hci_sco_hdr(ll->rx_skb);
  362. BT_DBG("SCO header: dlen %d", sh->dlen);
  363. ll_check_data_len(hu->hdev, ll, sh->dlen);
  364. continue;
  365. }
  366. }
  367. /* HCILL_W4_PACKET_TYPE */
  368. switch (*ptr) {
  369. case HCI_EVENT_PKT:
  370. BT_DBG("Event packet");
  371. ll->rx_state = HCILL_W4_EVENT_HDR;
  372. ll->rx_count = HCI_EVENT_HDR_SIZE;
  373. type = HCI_EVENT_PKT;
  374. break;
  375. case HCI_ACLDATA_PKT:
  376. BT_DBG("ACL packet");
  377. ll->rx_state = HCILL_W4_ACL_HDR;
  378. ll->rx_count = HCI_ACL_HDR_SIZE;
  379. type = HCI_ACLDATA_PKT;
  380. break;
  381. case HCI_SCODATA_PKT:
  382. BT_DBG("SCO packet");
  383. ll->rx_state = HCILL_W4_SCO_HDR;
  384. ll->rx_count = HCI_SCO_HDR_SIZE;
  385. type = HCI_SCODATA_PKT;
  386. break;
  387. /* HCILL signals */
  388. case HCILL_GO_TO_SLEEP_IND:
  389. BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
  390. ll_device_want_to_sleep(hu);
  391. ptr++; count--;
  392. continue;
  393. case HCILL_GO_TO_SLEEP_ACK:
  394. /* shouldn't happen */
  395. BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
  396. ptr++; count--;
  397. continue;
  398. case HCILL_WAKE_UP_IND:
  399. BT_DBG("HCILL_WAKE_UP_IND packet");
  400. ll_device_want_to_wakeup(hu);
  401. ptr++; count--;
  402. continue;
  403. case HCILL_WAKE_UP_ACK:
  404. BT_DBG("HCILL_WAKE_UP_ACK packet");
  405. ll_device_woke_up(hu);
  406. ptr++; count--;
  407. continue;
  408. default:
  409. BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
  410. hu->hdev->stat.err_rx++;
  411. ptr++; count--;
  412. continue;
  413. }
  414. ptr++; count--;
  415. /* Allocate packet */
  416. ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  417. if (!ll->rx_skb) {
  418. BT_ERR("Can't allocate mem for new packet");
  419. ll->rx_state = HCILL_W4_PACKET_TYPE;
  420. ll->rx_count = 0;
  421. return -ENOMEM;
  422. }
  423. hci_skb_pkt_type(ll->rx_skb) = type;
  424. }
  425. return count;
  426. }
  427. static struct sk_buff *ll_dequeue(struct hci_uart *hu)
  428. {
  429. struct ll_struct *ll = hu->priv;
  430. return skb_dequeue(&ll->txq);
  431. }
  432. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  433. static int read_local_version(struct hci_dev *hdev)
  434. {
  435. int err = 0;
  436. unsigned short version = 0;
  437. struct sk_buff *skb;
  438. struct hci_rp_read_local_version *ver;
  439. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
  440. if (IS_ERR(skb)) {
  441. bt_dev_err(hdev, "Reading TI version information failed (%ld)",
  442. PTR_ERR(skb));
  443. return PTR_ERR(skb);
  444. }
  445. if (skb->len != sizeof(*ver)) {
  446. err = -EILSEQ;
  447. goto out;
  448. }
  449. ver = (struct hci_rp_read_local_version *)skb->data;
  450. if (le16_to_cpu(ver->manufacturer) != 13) {
  451. err = -ENODEV;
  452. goto out;
  453. }
  454. version = le16_to_cpu(ver->lmp_subver);
  455. out:
  456. if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
  457. kfree_skb(skb);
  458. return err ? err : version;
  459. }
  460. /**
  461. * download_firmware -
  462. * internal function which parses through the .bts firmware
  463. * script file intreprets SEND, DELAY actions only as of now
  464. */
  465. static int download_firmware(struct ll_device *lldev)
  466. {
  467. unsigned short chip, min_ver, maj_ver;
  468. int version, err, len;
  469. unsigned char *ptr, *action_ptr;
  470. unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
  471. const struct firmware *fw;
  472. struct sk_buff *skb;
  473. struct hci_command *cmd;
  474. version = read_local_version(lldev->hu.hdev);
  475. if (version < 0)
  476. return version;
  477. chip = (version & 0x7C00) >> 10;
  478. min_ver = (version & 0x007F);
  479. maj_ver = (version & 0x0380) >> 7;
  480. if (version & 0x8000)
  481. maj_ver |= 0x0008;
  482. snprintf(bts_scr_name, sizeof(bts_scr_name),
  483. "ti-connectivity/TIInit_%d.%d.%d.bts",
  484. chip, maj_ver, min_ver);
  485. err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
  486. if (err || !fw->data || !fw->size) {
  487. bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
  488. err, bts_scr_name);
  489. return -EINVAL;
  490. }
  491. ptr = (void *)fw->data;
  492. len = fw->size;
  493. /* bts_header to remove out magic number and
  494. * version
  495. */
  496. ptr += sizeof(struct bts_header);
  497. len -= sizeof(struct bts_header);
  498. while (len > 0 && ptr) {
  499. bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
  500. ((struct bts_action *)ptr)->size,
  501. ((struct bts_action *)ptr)->type);
  502. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  503. switch (((struct bts_action *)ptr)->type) {
  504. case ACTION_SEND_COMMAND: /* action send */
  505. bt_dev_dbg(lldev->hu.hdev, "S");
  506. cmd = (struct hci_command *)action_ptr;
  507. if (cmd->opcode == 0xff36) {
  508. /* ignore remote change
  509. * baud rate HCI VS command
  510. */
  511. bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
  512. break;
  513. }
  514. if (cmd->prefix != 1)
  515. bt_dev_dbg(lldev->hu.hdev, "command type %d\n", cmd->prefix);
  516. skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
  517. if (IS_ERR(skb)) {
  518. bt_dev_err(lldev->hu.hdev, "send command failed\n");
  519. err = PTR_ERR(skb);
  520. goto out_rel_fw;
  521. }
  522. kfree_skb(skb);
  523. break;
  524. case ACTION_WAIT_EVENT: /* wait */
  525. /* no need to wait as command was synchronous */
  526. bt_dev_dbg(lldev->hu.hdev, "W");
  527. break;
  528. case ACTION_DELAY: /* sleep */
  529. bt_dev_info(lldev->hu.hdev, "sleep command in scr");
  530. mdelay(((struct bts_action_delay *)action_ptr)->msec);
  531. break;
  532. }
  533. len -= (sizeof(struct bts_action) +
  534. ((struct bts_action *)ptr)->size);
  535. ptr += sizeof(struct bts_action) +
  536. ((struct bts_action *)ptr)->size;
  537. }
  538. out_rel_fw:
  539. /* fw download complete */
  540. release_firmware(fw);
  541. return err;
  542. }
  543. static int ll_setup(struct hci_uart *hu)
  544. {
  545. int err, retry = 3;
  546. struct ll_device *lldev;
  547. struct serdev_device *serdev = hu->serdev;
  548. u32 speed;
  549. if (!serdev)
  550. return 0;
  551. lldev = serdev_device_get_drvdata(serdev);
  552. serdev_device_set_flow_control(serdev, true);
  553. do {
  554. /* Configure BT_EN to HIGH state */
  555. gpiod_set_value_cansleep(lldev->enable_gpio, 0);
  556. msleep(5);
  557. gpiod_set_value_cansleep(lldev->enable_gpio, 1);
  558. msleep(100);
  559. err = download_firmware(lldev);
  560. if (!err)
  561. break;
  562. /* Toggle BT_EN and retry */
  563. bt_dev_err(hu->hdev, "download firmware failed, retrying...");
  564. } while (retry--);
  565. if (err)
  566. return err;
  567. /* Operational speed if any */
  568. if (hu->oper_speed)
  569. speed = hu->oper_speed;
  570. else if (hu->proto->oper_speed)
  571. speed = hu->proto->oper_speed;
  572. else
  573. speed = 0;
  574. if (speed) {
  575. struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT);
  576. if (!IS_ERR(skb)) {
  577. kfree_skb(skb);
  578. serdev_device_set_baudrate(serdev, speed);
  579. }
  580. }
  581. return 0;
  582. }
  583. static const struct hci_uart_proto llp;
  584. static int hci_ti_probe(struct serdev_device *serdev)
  585. {
  586. struct hci_uart *hu;
  587. struct ll_device *lldev;
  588. u32 max_speed = 3000000;
  589. lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
  590. if (!lldev)
  591. return -ENOMEM;
  592. hu = &lldev->hu;
  593. serdev_device_set_drvdata(serdev, lldev);
  594. lldev->serdev = hu->serdev = serdev;
  595. lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
  596. if (IS_ERR(lldev->enable_gpio))
  597. return PTR_ERR(lldev->enable_gpio);
  598. lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
  599. if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
  600. return PTR_ERR(lldev->ext_clk);
  601. of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
  602. hci_uart_set_speeds(hu, 115200, max_speed);
  603. return hci_uart_register_device(hu, &llp);
  604. }
  605. static void hci_ti_remove(struct serdev_device *serdev)
  606. {
  607. struct ll_device *lldev = serdev_device_get_drvdata(serdev);
  608. hci_uart_unregister_device(&lldev->hu);
  609. }
  610. static const struct of_device_id hci_ti_of_match[] = {
  611. { .compatible = "ti,wl1271-st" },
  612. { .compatible = "ti,wl1273-st" },
  613. { .compatible = "ti,wl1281-st" },
  614. { .compatible = "ti,wl1283-st" },
  615. { .compatible = "ti,wl1285-st" },
  616. { .compatible = "ti,wl1801-st" },
  617. { .compatible = "ti,wl1805-st" },
  618. { .compatible = "ti,wl1807-st" },
  619. { .compatible = "ti,wl1831-st" },
  620. { .compatible = "ti,wl1835-st" },
  621. { .compatible = "ti,wl1837-st" },
  622. {},
  623. };
  624. MODULE_DEVICE_TABLE(of, hci_ti_of_match);
  625. static struct serdev_device_driver hci_ti_drv = {
  626. .driver = {
  627. .name = "hci-ti",
  628. .of_match_table = of_match_ptr(hci_ti_of_match),
  629. },
  630. .probe = hci_ti_probe,
  631. .remove = hci_ti_remove,
  632. };
  633. #else
  634. #define ll_setup NULL
  635. #endif
  636. static const struct hci_uart_proto llp = {
  637. .id = HCI_UART_LL,
  638. .name = "LL",
  639. .setup = ll_setup,
  640. .open = ll_open,
  641. .close = ll_close,
  642. .recv = ll_recv,
  643. .enqueue = ll_enqueue,
  644. .dequeue = ll_dequeue,
  645. .flush = ll_flush,
  646. };
  647. int __init ll_init(void)
  648. {
  649. serdev_device_driver_register(&hci_ti_drv);
  650. return hci_uart_register_proto(&llp);
  651. }
  652. int __exit ll_deinit(void)
  653. {
  654. serdev_device_driver_unregister(&hci_ti_drv);
  655. return hci_uart_unregister_proto(&llp);
  656. }