1 // SPDX-License-Identifier: GPL-2.0-only
3 * Texas Instruments' Bluetooth HCILL UART protocol
5 * HCILL (HCI Low Level) is a Texas Instruments' power management
6 * protocol extension to H4.
8 * Copyright (C) 2007 Texas Instruments, Inc.
10 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
13 * This file is based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/firmware.h>
25 #include <linux/interrupt.h>
26 #include <linux/ptrace.h>
27 #include <linux/poll.h>
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/signal.h>
33 #include <linux/ioctl.h>
35 #include <linux/serdev.h>
36 #include <linux/skbuff.h>
37 #include <linux/ti_wilink_st.h>
38 #include <linux/clk.h>
40 #include <net/bluetooth/bluetooth.h>
41 #include <net/bluetooth/hci_core.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/nvmem-consumer.h>
47 /* Vendor-specific HCI commands */
48 #define HCI_VS_WRITE_BD_ADDR 0xfc06
49 #define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
52 #define HCILL_GO_TO_SLEEP_IND 0x30
53 #define HCILL_GO_TO_SLEEP_ACK 0x31
54 #define HCILL_WAKE_UP_IND 0x32
55 #define HCILL_WAKE_UP_ACK 0x33
60 HCILL_ASLEEP_TO_AWAKE,
67 struct serdev_device *serdev;
68 struct gpio_desc *enable_gpio;
74 struct sk_buff *rx_skb;
75 struct sk_buff_head txq;
76 spinlock_t hcill_lock; /* HCILL state lock */
77 unsigned long hcill_state; /* HCILL power state */
78 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
82 * Builds and sends an HCILL command packet.
83 * These are very simple packets with only 1 cmd byte
85 static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
88 struct sk_buff *skb = NULL;
89 struct ll_struct *ll = hu->priv;
91 BT_DBG("hu %p cmd 0x%x", hu, cmd);
94 skb = bt_skb_alloc(1, GFP_ATOMIC);
96 BT_ERR("cannot allocate memory for HCILL packet");
102 skb_put_u8(skb, cmd);
105 skb_queue_tail(&ll->txq, skb);
110 /* Initialize protocol */
111 static int ll_open(struct hci_uart *hu)
113 struct ll_struct *ll;
117 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
121 skb_queue_head_init(&ll->txq);
122 skb_queue_head_init(&ll->tx_wait_q);
123 spin_lock_init(&ll->hcill_lock);
125 ll->hcill_state = HCILL_AWAKE;
130 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
132 if (!IS_ERR(lldev->ext_clk))
133 clk_prepare_enable(lldev->ext_clk);
139 /* Flush protocol data */
140 static int ll_flush(struct hci_uart *hu)
142 struct ll_struct *ll = hu->priv;
146 skb_queue_purge(&ll->tx_wait_q);
147 skb_queue_purge(&ll->txq);
153 static int ll_close(struct hci_uart *hu)
155 struct ll_struct *ll = hu->priv;
159 skb_queue_purge(&ll->tx_wait_q);
160 skb_queue_purge(&ll->txq);
162 kfree_skb(ll->rx_skb);
165 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
167 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
169 clk_disable_unprepare(lldev->ext_clk);
180 * internal function, which does common work of the device wake up process:
181 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
182 * 2. changes internal state to HCILL_AWAKE.
183 * Note: assumes that hcill_lock spinlock is taken,
184 * shouldn't be called otherwise!
186 static void __ll_do_awake(struct ll_struct *ll)
188 struct sk_buff *skb = NULL;
190 while ((skb = skb_dequeue(&ll->tx_wait_q)))
191 skb_queue_tail(&ll->txq, skb);
193 ll->hcill_state = HCILL_AWAKE;
197 * Called upon a wake-up-indication from the device
199 static void ll_device_want_to_wakeup(struct hci_uart *hu)
202 struct ll_struct *ll = hu->priv;
206 /* lock hcill state */
207 spin_lock_irqsave(&ll->hcill_lock, flags);
209 switch (ll->hcill_state) {
210 case HCILL_ASLEEP_TO_AWAKE:
212 * This state means that both the host and the BRF chip
213 * have simultaneously sent a wake-up-indication packet.
214 * Traditionally, in this case, receiving a wake-up-indication
215 * was enough and an additional wake-up-ack wasn't needed.
216 * This has changed with the BRF6350, which does require an
217 * explicit wake-up-ack. Other BRF versions, which do not
218 * require an explicit ack here, do accept it, thus it is
219 * perfectly safe to always send one.
221 BT_DBG("dual wake-up-indication");
224 /* acknowledge device wake up */
225 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
226 BT_ERR("cannot acknowledge device wake up");
231 /* any other state is illegal */
232 BT_ERR("received HCILL_WAKE_UP_IND in state %ld",
237 /* send pending packets and change state to HCILL_AWAKE */
241 spin_unlock_irqrestore(&ll->hcill_lock, flags);
243 /* actually send the packets */
244 hci_uart_tx_wakeup(hu);
248 * Called upon a sleep-indication from the device
250 static void ll_device_want_to_sleep(struct hci_uart *hu)
253 struct ll_struct *ll = hu->priv;
257 /* lock hcill state */
258 spin_lock_irqsave(&ll->hcill_lock, flags);
261 if (ll->hcill_state != HCILL_AWAKE)
262 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld",
265 /* acknowledge device sleep */
266 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
267 BT_ERR("cannot acknowledge device sleep");
272 ll->hcill_state = HCILL_ASLEEP;
275 spin_unlock_irqrestore(&ll->hcill_lock, flags);
277 /* actually send the sleep ack packet */
278 hci_uart_tx_wakeup(hu);
282 * Called upon wake-up-acknowledgement from the device
284 static void ll_device_woke_up(struct hci_uart *hu)
287 struct ll_struct *ll = hu->priv;
291 /* lock hcill state */
292 spin_lock_irqsave(&ll->hcill_lock, flags);
295 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
296 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld",
299 /* send pending packets and change state to HCILL_AWAKE */
302 spin_unlock_irqrestore(&ll->hcill_lock, flags);
304 /* actually send the packets */
305 hci_uart_tx_wakeup(hu);
308 /* Enqueue frame for transmittion (padding, crc, etc) */
309 /* may be called from two simultaneous tasklets */
310 static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
312 unsigned long flags = 0;
313 struct ll_struct *ll = hu->priv;
315 BT_DBG("hu %p skb %p", hu, skb);
317 /* Prepend skb with frame type */
318 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
320 /* lock hcill state */
321 spin_lock_irqsave(&ll->hcill_lock, flags);
323 /* act according to current state */
324 switch (ll->hcill_state) {
326 BT_DBG("device awake, sending normally");
327 skb_queue_tail(&ll->txq, skb);
330 BT_DBG("device asleep, waking up and queueing packet");
331 /* save packet for later */
332 skb_queue_tail(&ll->tx_wait_q, skb);
334 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
335 BT_ERR("cannot wake up device");
338 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
340 case HCILL_ASLEEP_TO_AWAKE:
341 BT_DBG("device waking up, queueing packet");
342 /* transient state; just keep packet for later */
343 skb_queue_tail(&ll->tx_wait_q, skb);
346 BT_ERR("illegal hcill state: %ld (losing packet)",
352 spin_unlock_irqrestore(&ll->hcill_lock, flags);
357 static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
359 struct hci_uart *hu = hci_get_drvdata(hdev);
360 struct ll_struct *ll = hu->priv;
362 switch (hci_skb_pkt_type(skb)) {
363 case HCILL_GO_TO_SLEEP_IND:
364 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
365 ll_device_want_to_sleep(hu);
367 case HCILL_GO_TO_SLEEP_ACK:
368 /* shouldn't happen */
369 bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
372 case HCILL_WAKE_UP_IND:
373 BT_DBG("HCILL_WAKE_UP_IND packet");
374 ll_device_want_to_wakeup(hu);
376 case HCILL_WAKE_UP_ACK:
377 BT_DBG("HCILL_WAKE_UP_ACK packet");
378 ll_device_woke_up(hu);
386 #define LL_RECV_SLEEP_IND \
387 .type = HCILL_GO_TO_SLEEP_IND, \
393 #define LL_RECV_SLEEP_ACK \
394 .type = HCILL_GO_TO_SLEEP_ACK, \
400 #define LL_RECV_WAKE_IND \
401 .type = HCILL_WAKE_UP_IND, \
407 #define LL_RECV_WAKE_ACK \
408 .type = HCILL_WAKE_UP_ACK, \
414 static const struct h4_recv_pkt ll_recv_pkts[] = {
415 { H4_RECV_ACL, .recv = hci_recv_frame },
416 { H4_RECV_SCO, .recv = hci_recv_frame },
417 { H4_RECV_EVENT, .recv = hci_recv_frame },
418 { LL_RECV_SLEEP_IND, .recv = ll_recv_frame },
419 { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame },
420 { LL_RECV_WAKE_IND, .recv = ll_recv_frame },
421 { LL_RECV_WAKE_ACK, .recv = ll_recv_frame },
425 static int ll_recv(struct hci_uart *hu, const void *data, int count)
427 struct ll_struct *ll = hu->priv;
429 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
432 ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
433 ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
434 if (IS_ERR(ll->rx_skb)) {
435 int err = PTR_ERR(ll->rx_skb);
436 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
444 static struct sk_buff *ll_dequeue(struct hci_uart *hu)
446 struct ll_struct *ll = hu->priv;
448 return skb_dequeue(&ll->txq);
451 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
452 static int read_local_version(struct hci_dev *hdev)
455 unsigned short version = 0;
457 struct hci_rp_read_local_version *ver;
459 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
462 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
466 if (skb->len != sizeof(*ver)) {
471 ver = (struct hci_rp_read_local_version *)skb->data;
472 if (le16_to_cpu(ver->manufacturer) != 13) {
477 version = le16_to_cpu(ver->lmp_subver);
481 bt_dev_err(hdev, "Failed to read TI version info: %d", err);
483 return err ? err : version;
486 static int send_command_from_firmware(struct ll_device *lldev,
487 struct hci_command *cmd)
491 if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
492 /* ignore remote change
493 * baud rate HCI VS command
495 bt_dev_warn(lldev->hu.hdev,
496 "change remote baud rate command in firmware");
499 if (cmd->prefix != 1)
500 bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
502 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen,
503 &cmd->speed, HCI_INIT_TIMEOUT);
505 bt_dev_err(lldev->hu.hdev, "send command failed");
513 * download_firmware -
514 * internal function which parses through the .bts firmware
515 * script file intreprets SEND, DELAY actions only as of now
517 static int download_firmware(struct ll_device *lldev)
519 unsigned short chip, min_ver, maj_ver;
520 int version, err, len;
521 unsigned char *ptr, *action_ptr;
522 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
523 const struct firmware *fw;
524 struct hci_command *cmd;
526 version = read_local_version(lldev->hu.hdev);
530 chip = (version & 0x7C00) >> 10;
531 min_ver = (version & 0x007F);
532 maj_ver = (version & 0x0380) >> 7;
533 if (version & 0x8000)
536 snprintf(bts_scr_name, sizeof(bts_scr_name),
537 "ti-connectivity/TIInit_%d.%d.%d.bts",
538 chip, maj_ver, min_ver);
540 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
541 if (err || !fw->data || !fw->size) {
542 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
546 ptr = (void *)fw->data;
548 /* bts_header to remove out magic number and
551 ptr += sizeof(struct bts_header);
552 len -= sizeof(struct bts_header);
554 while (len > 0 && ptr) {
555 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
556 ((struct bts_action *)ptr)->size,
557 ((struct bts_action *)ptr)->type);
559 action_ptr = &(((struct bts_action *)ptr)->data[0]);
561 switch (((struct bts_action *)ptr)->type) {
562 case ACTION_SEND_COMMAND: /* action send */
563 bt_dev_dbg(lldev->hu.hdev, "S");
564 cmd = (struct hci_command *)action_ptr;
565 err = send_command_from_firmware(lldev, cmd);
569 case ACTION_WAIT_EVENT: /* wait */
570 /* no need to wait as command was synchronous */
571 bt_dev_dbg(lldev->hu.hdev, "W");
573 case ACTION_DELAY: /* sleep */
574 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
575 msleep(((struct bts_action_delay *)action_ptr)->msec);
578 len -= (sizeof(struct bts_action) +
579 ((struct bts_action *)ptr)->size);
580 ptr += sizeof(struct bts_action) +
581 ((struct bts_action *)ptr)->size;
585 /* fw download complete */
586 release_firmware(fw);
590 static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
592 bdaddr_t bdaddr_swapped;
595 /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
596 * address to be MSB first, but bdaddr_t has the convention of being
599 baswap(&bdaddr_swapped, bdaddr);
600 skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
601 &bdaddr_swapped, HCI_INIT_TIMEOUT);
605 return PTR_ERR_OR_ZERO(skb);
608 static int ll_setup(struct hci_uart *hu)
611 struct ll_device *lldev;
612 struct serdev_device *serdev = hu->serdev;
618 lldev = serdev_device_get_drvdata(serdev);
620 hu->hdev->set_bdaddr = ll_set_bdaddr;
622 serdev_device_set_flow_control(serdev, true);
625 /* Reset the Bluetooth device */
626 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
628 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
630 err = serdev_device_wait_for_cts(serdev, true, 200);
632 bt_dev_err(hu->hdev, "Failed to get CTS");
636 err = download_firmware(lldev);
640 /* Toggle BT_EN and retry */
641 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
647 /* Set BD address if one was specified at probe */
648 if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
649 /* This means that there was an error getting the BD address
650 * during probe, so mark the device as having a bad address.
652 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
653 } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
654 err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
656 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
659 /* Operational speed if any */
661 speed = hu->oper_speed;
662 else if (hu->proto->oper_speed)
663 speed = hu->proto->oper_speed;
668 __le32 speed_le = cpu_to_le32(speed);
671 skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
672 sizeof(speed_le), &speed_le,
676 serdev_device_set_baudrate(serdev, speed);
683 static const struct hci_uart_proto llp;
685 static int hci_ti_probe(struct serdev_device *serdev)
688 struct ll_device *lldev;
689 struct nvmem_cell *bdaddr_cell;
690 u32 max_speed = 3000000;
692 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
697 serdev_device_set_drvdata(serdev, lldev);
698 lldev->serdev = hu->serdev = serdev;
700 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev,
703 if (IS_ERR(lldev->enable_gpio))
704 return PTR_ERR(lldev->enable_gpio);
706 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
707 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
708 return PTR_ERR(lldev->ext_clk);
710 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
711 hci_uart_set_speeds(hu, 115200, max_speed);
713 /* optional BD address from nvram */
714 bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
715 if (IS_ERR(bdaddr_cell)) {
716 int err = PTR_ERR(bdaddr_cell);
718 if (err == -EPROBE_DEFER)
721 /* ENOENT means there is no matching nvmem cell and ENOSYS
722 * means that nvmem is not enabled in the kernel configuration.
724 if (err != -ENOENT && err != -ENOSYS) {
725 /* If there was some other error, give userspace a
726 * chance to fix the problem instead of failing to load
727 * the driver. Using BDADDR_NONE as a flag that is
728 * tested later in the setup function.
730 dev_warn(&serdev->dev,
731 "Failed to get \"bd-address\" nvmem cell (%d)\n",
733 bacpy(&lldev->bdaddr, BDADDR_NONE);
739 bdaddr = nvmem_cell_read(bdaddr_cell, &len);
740 nvmem_cell_put(bdaddr_cell);
741 if (IS_ERR(bdaddr)) {
742 dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
743 return PTR_ERR(bdaddr);
745 if (len != sizeof(bdaddr_t)) {
746 dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
751 /* As per the device tree bindings, the value from nvmem is
752 * expected to be MSB first, but in the kernel it is expected
753 * that bdaddr_t is LSB first.
755 baswap(&lldev->bdaddr, bdaddr);
759 return hci_uart_register_device(hu, &llp);
762 static void hci_ti_remove(struct serdev_device *serdev)
764 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
766 hci_uart_unregister_device(&lldev->hu);
769 static const struct of_device_id hci_ti_of_match[] = {
770 { .compatible = "ti,cc2560" },
771 { .compatible = "ti,wl1271-st" },
772 { .compatible = "ti,wl1273-st" },
773 { .compatible = "ti,wl1281-st" },
774 { .compatible = "ti,wl1283-st" },
775 { .compatible = "ti,wl1285-st" },
776 { .compatible = "ti,wl1801-st" },
777 { .compatible = "ti,wl1805-st" },
778 { .compatible = "ti,wl1807-st" },
779 { .compatible = "ti,wl1831-st" },
780 { .compatible = "ti,wl1835-st" },
781 { .compatible = "ti,wl1837-st" },
784 MODULE_DEVICE_TABLE(of, hci_ti_of_match);
786 static struct serdev_device_driver hci_ti_drv = {
789 .of_match_table = of_match_ptr(hci_ti_of_match),
791 .probe = hci_ti_probe,
792 .remove = hci_ti_remove,
795 #define ll_setup NULL
798 static const struct hci_uart_proto llp = {
805 .enqueue = ll_enqueue,
806 .dequeue = ll_dequeue,
810 int __init ll_init(void)
812 serdev_device_driver_register(&hci_ti_drv);
814 return hci_uart_register_proto(&llp);
817 int __exit ll_deinit(void)
819 serdev_device_driver_unregister(&hci_ti_drv);
821 return hci_uart_unregister_proto(&llp);