hci_ll.c 18 KB

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