2 * Texas Instruments' Bluetooth HCILL UART protocol
4 * HCILL (HCI Low Level) is a Texas Instruments' power management
5 * protocol extension to H4.
7 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
12 * This file is based on hci_h4.c, which was written
13 * by Maxim Krasnyansky and Marcel Holtmann.
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
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.
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
30 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #include <linux/types.h>
36 #include <linux/fcntl.h>
37 #include <linux/firmware.h>
38 #include <linux/interrupt.h>
39 #include <linux/ptrace.h>
40 #include <linux/poll.h>
42 #include <linux/slab.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/signal.h>
46 #include <linux/ioctl.h>
48 #include <linux/serdev.h>
49 #include <linux/skbuff.h>
50 #include <linux/ti_wilink_st.h>
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
54 #include <linux/gpio/consumer.h>
59 #define HCILL_GO_TO_SLEEP_IND 0x30
60 #define HCILL_GO_TO_SLEEP_ACK 0x31
61 #define HCILL_WAKE_UP_IND 0x32
62 #define HCILL_WAKE_UP_ACK 0x33
64 /* HCILL receiver States */
65 #define HCILL_W4_PACKET_TYPE 0
66 #define HCILL_W4_EVENT_HDR 1
67 #define HCILL_W4_ACL_HDR 2
68 #define HCILL_W4_SCO_HDR 3
69 #define HCILL_W4_DATA 4
74 HCILL_ASLEEP_TO_AWAKE,
85 struct serdev_device *serdev;
86 struct gpio_desc *enable_gpio;
90 unsigned long rx_state;
91 unsigned long rx_count;
92 struct sk_buff *rx_skb;
93 struct sk_buff_head txq;
94 spinlock_t hcill_lock; /* HCILL state lock */
95 unsigned long hcill_state; /* HCILL power state */
96 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
100 * Builds and sends an HCILL command packet.
101 * These are very simple packets with only 1 cmd byte
103 static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
106 struct sk_buff *skb = NULL;
107 struct ll_struct *ll = hu->priv;
108 struct hcill_cmd *hcill_packet;
110 BT_DBG("hu %p cmd 0x%x", hu, cmd);
112 /* allocate packet */
113 skb = bt_skb_alloc(1, GFP_ATOMIC);
115 BT_ERR("cannot allocate memory for HCILL packet");
121 hcill_packet = (struct hcill_cmd *) skb_put(skb, 1);
122 hcill_packet->cmd = cmd;
125 skb_queue_tail(&ll->txq, skb);
130 /* Initialize protocol */
131 static int ll_open(struct hci_uart *hu)
133 struct ll_struct *ll;
137 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
141 skb_queue_head_init(&ll->txq);
142 skb_queue_head_init(&ll->tx_wait_q);
143 spin_lock_init(&ll->hcill_lock);
145 ll->hcill_state = HCILL_AWAKE;
150 serdev_device_open(hu->serdev);
155 /* Flush protocol data */
156 static int ll_flush(struct hci_uart *hu)
158 struct ll_struct *ll = hu->priv;
162 skb_queue_purge(&ll->tx_wait_q);
163 skb_queue_purge(&ll->txq);
169 static int ll_close(struct hci_uart *hu)
171 struct ll_struct *ll = hu->priv;
175 skb_queue_purge(&ll->tx_wait_q);
176 skb_queue_purge(&ll->txq);
178 kfree_skb(ll->rx_skb);
181 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
182 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
184 serdev_device_close(hu->serdev);
195 * internal function, which does common work of the device wake up process:
196 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
197 * 2. changes internal state to HCILL_AWAKE.
198 * Note: assumes that hcill_lock spinlock is taken,
199 * shouldn't be called otherwise!
201 static void __ll_do_awake(struct ll_struct *ll)
203 struct sk_buff *skb = NULL;
205 while ((skb = skb_dequeue(&ll->tx_wait_q)))
206 skb_queue_tail(&ll->txq, skb);
208 ll->hcill_state = HCILL_AWAKE;
212 * Called upon a wake-up-indication from the device
214 static void ll_device_want_to_wakeup(struct hci_uart *hu)
217 struct ll_struct *ll = hu->priv;
221 /* lock hcill state */
222 spin_lock_irqsave(&ll->hcill_lock, flags);
224 switch (ll->hcill_state) {
225 case HCILL_ASLEEP_TO_AWAKE:
227 * This state means that both the host and the BRF chip
228 * have simultaneously sent a wake-up-indication packet.
229 * Traditionally, in this case, receiving a wake-up-indication
230 * was enough and an additional wake-up-ack wasn't needed.
231 * This has changed with the BRF6350, which does require an
232 * explicit wake-up-ack. Other BRF versions, which do not
233 * require an explicit ack here, do accept it, thus it is
234 * perfectly safe to always send one.
236 BT_DBG("dual wake-up-indication");
237 /* deliberate fall-through - do not add break */
239 /* acknowledge device wake up */
240 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
241 BT_ERR("cannot acknowledge device wake up");
246 /* any other state is illegal */
247 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
251 /* send pending packets and change state to HCILL_AWAKE */
255 spin_unlock_irqrestore(&ll->hcill_lock, flags);
257 /* actually send the packets */
258 hci_uart_tx_wakeup(hu);
262 * Called upon a sleep-indication from the device
264 static void ll_device_want_to_sleep(struct hci_uart *hu)
267 struct ll_struct *ll = hu->priv;
271 /* lock hcill state */
272 spin_lock_irqsave(&ll->hcill_lock, flags);
275 if (ll->hcill_state != HCILL_AWAKE)
276 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
278 /* acknowledge device sleep */
279 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
280 BT_ERR("cannot acknowledge device sleep");
285 ll->hcill_state = HCILL_ASLEEP;
288 spin_unlock_irqrestore(&ll->hcill_lock, flags);
290 /* actually send the sleep ack packet */
291 hci_uart_tx_wakeup(hu);
295 * Called upon wake-up-acknowledgement from the device
297 static void ll_device_woke_up(struct hci_uart *hu)
300 struct ll_struct *ll = hu->priv;
304 /* lock hcill state */
305 spin_lock_irqsave(&ll->hcill_lock, flags);
308 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
309 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
311 /* send pending packets and change state to HCILL_AWAKE */
314 spin_unlock_irqrestore(&ll->hcill_lock, flags);
316 /* actually send the packets */
317 hci_uart_tx_wakeup(hu);
320 /* Enqueue frame for transmittion (padding, crc, etc) */
321 /* may be called from two simultaneous tasklets */
322 static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
324 unsigned long flags = 0;
325 struct ll_struct *ll = hu->priv;
327 BT_DBG("hu %p skb %p", hu, skb);
329 /* Prepend skb with frame type */
330 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
332 /* lock hcill state */
333 spin_lock_irqsave(&ll->hcill_lock, flags);
335 /* act according to current state */
336 switch (ll->hcill_state) {
338 BT_DBG("device awake, sending normally");
339 skb_queue_tail(&ll->txq, skb);
342 BT_DBG("device asleep, waking up and queueing packet");
343 /* save packet for later */
344 skb_queue_tail(&ll->tx_wait_q, skb);
346 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
347 BT_ERR("cannot wake up device");
350 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
352 case HCILL_ASLEEP_TO_AWAKE:
353 BT_DBG("device waking up, queueing packet");
354 /* transient state; just keep packet for later */
355 skb_queue_tail(&ll->tx_wait_q, skb);
358 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
363 spin_unlock_irqrestore(&ll->hcill_lock, flags);
368 static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
370 int room = skb_tailroom(ll->rx_skb);
372 BT_DBG("len %d room %d", len, room);
375 hci_recv_frame(hdev, ll->rx_skb);
376 } else if (len > room) {
377 BT_ERR("Data length is too large");
378 kfree_skb(ll->rx_skb);
380 ll->rx_state = HCILL_W4_DATA;
385 ll->rx_state = HCILL_W4_PACKET_TYPE;
393 static int ll_recv(struct hci_uart *hu, const void *data, int count)
395 struct ll_struct *ll = hu->priv;
397 struct hci_event_hdr *eh;
398 struct hci_acl_hdr *ah;
399 struct hci_sco_hdr *sh;
402 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
407 len = min_t(unsigned int, ll->rx_count, count);
408 memcpy(skb_put(ll->rx_skb, len), ptr, len);
409 ll->rx_count -= len; count -= len; ptr += len;
414 switch (ll->rx_state) {
416 BT_DBG("Complete data");
417 hci_recv_frame(hu->hdev, ll->rx_skb);
419 ll->rx_state = HCILL_W4_PACKET_TYPE;
423 case HCILL_W4_EVENT_HDR:
424 eh = hci_event_hdr(ll->rx_skb);
426 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
428 ll_check_data_len(hu->hdev, ll, eh->plen);
431 case HCILL_W4_ACL_HDR:
432 ah = hci_acl_hdr(ll->rx_skb);
433 dlen = __le16_to_cpu(ah->dlen);
435 BT_DBG("ACL header: dlen %d", dlen);
437 ll_check_data_len(hu->hdev, ll, dlen);
440 case HCILL_W4_SCO_HDR:
441 sh = hci_sco_hdr(ll->rx_skb);
443 BT_DBG("SCO header: dlen %d", sh->dlen);
445 ll_check_data_len(hu->hdev, ll, sh->dlen);
450 /* HCILL_W4_PACKET_TYPE */
453 BT_DBG("Event packet");
454 ll->rx_state = HCILL_W4_EVENT_HDR;
455 ll->rx_count = HCI_EVENT_HDR_SIZE;
456 type = HCI_EVENT_PKT;
459 case HCI_ACLDATA_PKT:
460 BT_DBG("ACL packet");
461 ll->rx_state = HCILL_W4_ACL_HDR;
462 ll->rx_count = HCI_ACL_HDR_SIZE;
463 type = HCI_ACLDATA_PKT;
466 case HCI_SCODATA_PKT:
467 BT_DBG("SCO packet");
468 ll->rx_state = HCILL_W4_SCO_HDR;
469 ll->rx_count = HCI_SCO_HDR_SIZE;
470 type = HCI_SCODATA_PKT;
474 case HCILL_GO_TO_SLEEP_IND:
475 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
476 ll_device_want_to_sleep(hu);
480 case HCILL_GO_TO_SLEEP_ACK:
481 /* shouldn't happen */
482 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
486 case HCILL_WAKE_UP_IND:
487 BT_DBG("HCILL_WAKE_UP_IND packet");
488 ll_device_want_to_wakeup(hu);
492 case HCILL_WAKE_UP_ACK:
493 BT_DBG("HCILL_WAKE_UP_ACK packet");
494 ll_device_woke_up(hu);
499 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
500 hu->hdev->stat.err_rx++;
507 /* Allocate packet */
508 ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
510 BT_ERR("Can't allocate mem for new packet");
511 ll->rx_state = HCILL_W4_PACKET_TYPE;
516 hci_skb_pkt_type(ll->rx_skb) = type;
522 static struct sk_buff *ll_dequeue(struct hci_uart *hu)
524 struct ll_struct *ll = hu->priv;
525 return skb_dequeue(&ll->txq);
528 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
529 static int read_local_version(struct hci_dev *hdev)
532 unsigned short version = 0;
534 struct hci_rp_read_local_version *ver;
536 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
538 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
542 if (skb->len != sizeof(*ver)) {
547 ver = (struct hci_rp_read_local_version *)skb->data;
548 if (le16_to_cpu(ver->manufacturer) != 13) {
553 version = le16_to_cpu(ver->lmp_subver);
556 if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
558 return err ? err : version;
562 * download_firmware -
563 * internal function which parses through the .bts firmware
564 * script file intreprets SEND, DELAY actions only as of now
566 static int download_firmware(struct ll_device *lldev)
568 unsigned short chip, min_ver, maj_ver;
569 int version, err, len;
570 unsigned char *ptr, *action_ptr;
571 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
572 const struct firmware *fw;
574 struct hci_command *cmd;
576 version = read_local_version(lldev->hu.hdev);
580 chip = (version & 0x7C00) >> 10;
581 min_ver = (version & 0x007F);
582 maj_ver = (version & 0x0380) >> 7;
583 if (version & 0x8000)
586 snprintf(bts_scr_name, sizeof(bts_scr_name),
587 "ti-connectivity/TIInit_%d.%d.%d.bts",
588 chip, maj_ver, min_ver);
590 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
591 if (err || !fw->data || !fw->size) {
592 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
596 ptr = (void *)fw->data;
598 /* bts_header to remove out magic number and
601 ptr += sizeof(struct bts_header);
602 len -= sizeof(struct bts_header);
604 while (len > 0 && ptr) {
605 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
606 ((struct bts_action *)ptr)->size,
607 ((struct bts_action *)ptr)->type);
609 action_ptr = &(((struct bts_action *)ptr)->data[0]);
611 switch (((struct bts_action *)ptr)->type) {
612 case ACTION_SEND_COMMAND: /* action send */
613 bt_dev_dbg(lldev->hu.hdev, "S");
614 cmd = (struct hci_command *)action_ptr;
615 if (cmd->opcode == 0xff36) {
616 /* ignore remote change
617 * baud rate HCI VS command */
618 bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
621 if (cmd->prefix != 1)
622 bt_dev_dbg(lldev->hu.hdev, "command type %d\n", cmd->prefix);
624 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
626 bt_dev_err(lldev->hu.hdev, "send command failed\n");
631 case ACTION_WAIT_EVENT: /* wait */
632 /* no need to wait as command was synchronous */
633 bt_dev_dbg(lldev->hu.hdev, "W");
635 case ACTION_DELAY: /* sleep */
636 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
637 mdelay(((struct bts_action_delay *)action_ptr)->msec);
640 len -= (sizeof(struct bts_action) +
641 ((struct bts_action *)ptr)->size);
642 ptr += sizeof(struct bts_action) +
643 ((struct bts_action *)ptr)->size;
647 /* fw download complete */
648 release_firmware(fw);
652 static int ll_setup(struct hci_uart *hu)
655 struct ll_device *lldev;
656 struct serdev_device *serdev = hu->serdev;
662 lldev = serdev_device_get_drvdata(serdev);
664 serdev_device_set_flow_control(serdev, true);
667 /* Configure BT_EN to HIGH state */
668 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
670 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
673 err = download_firmware(lldev);
677 /* Toggle BT_EN and retry */
678 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
684 /* Operational speed if any */
686 speed = hu->oper_speed;
687 else if (hu->proto->oper_speed)
688 speed = hu->proto->oper_speed;
693 struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT);
696 serdev_device_set_baudrate(serdev, speed);
703 static const struct hci_uart_proto llp;
705 static int hci_ti_probe(struct serdev_device *serdev)
708 struct ll_device *lldev;
709 u32 max_speed = 3000000;
711 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
716 serdev_device_set_drvdata(serdev, lldev);
717 lldev->serdev = hu->serdev = serdev;
719 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
720 if (IS_ERR(lldev->enable_gpio))
721 return PTR_ERR(lldev->enable_gpio);
723 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
724 hci_uart_set_speeds(hu, 115200, max_speed);
726 return hci_uart_register_device(hu, &llp);
729 static void hci_ti_remove(struct serdev_device *serdev)
731 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
732 struct hci_uart *hu = &lldev->hu;
733 struct hci_dev *hdev = hu->hdev;
735 cancel_work_sync(&hu->write_work);
737 hci_unregister_dev(hdev);
739 hu->proto->close(hu);
742 static const struct of_device_id hci_ti_of_match[] = {
743 { .compatible = "ti,wl1831-st" },
744 { .compatible = "ti,wl1835-st" },
745 { .compatible = "ti,wl1837-st" },
748 MODULE_DEVICE_TABLE(of, hci_ti_of_match);
750 static struct serdev_device_driver hci_ti_drv = {
753 .of_match_table = of_match_ptr(hci_ti_of_match),
755 .probe = hci_ti_probe,
756 .remove = hci_ti_remove,
759 #define ll_setup NULL
762 static const struct hci_uart_proto llp = {
769 .enqueue = ll_enqueue,
770 .dequeue = ll_dequeue,
774 int __init ll_init(void)
776 serdev_device_driver_register(&hci_ti_drv);
778 return hci_uart_register_proto(&llp);
781 int __exit ll_deinit(void)
783 serdev_device_driver_unregister(&hci_ti_drv);
785 return hci_uart_unregister_proto(&llp);