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>
51 #include <linux/clk.h>
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
55 #include <linux/gpio/consumer.h>
60 #define HCILL_GO_TO_SLEEP_IND 0x30
61 #define HCILL_GO_TO_SLEEP_ACK 0x31
62 #define HCILL_WAKE_UP_IND 0x32
63 #define HCILL_WAKE_UP_ACK 0x33
65 /* HCILL receiver States */
66 #define HCILL_W4_PACKET_TYPE 0
67 #define HCILL_W4_EVENT_HDR 1
68 #define HCILL_W4_ACL_HDR 2
69 #define HCILL_W4_SCO_HDR 3
70 #define HCILL_W4_DATA 4
75 HCILL_ASLEEP_TO_AWAKE
,
86 struct serdev_device
*serdev
;
87 struct gpio_desc
*enable_gpio
;
92 unsigned long rx_state
;
93 unsigned long rx_count
;
94 struct sk_buff
*rx_skb
;
95 struct sk_buff_head txq
;
96 spinlock_t hcill_lock
; /* HCILL state lock */
97 unsigned long hcill_state
; /* HCILL power state */
98 struct sk_buff_head tx_wait_q
; /* HCILL wait queue */
102 * Builds and sends an HCILL command packet.
103 * These are very simple packets with only 1 cmd byte
105 static int send_hcill_cmd(u8 cmd
, struct hci_uart
*hu
)
108 struct sk_buff
*skb
= NULL
;
109 struct ll_struct
*ll
= hu
->priv
;
110 struct hcill_cmd
*hcill_packet
;
112 BT_DBG("hu %p cmd 0x%x", hu
, cmd
);
114 /* allocate packet */
115 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
117 BT_ERR("cannot allocate memory for HCILL packet");
123 hcill_packet
= skb_put(skb
, 1);
124 hcill_packet
->cmd
= cmd
;
127 skb_queue_tail(&ll
->txq
, skb
);
132 /* Initialize protocol */
133 static int ll_open(struct hci_uart
*hu
)
135 struct ll_struct
*ll
;
139 ll
= kzalloc(sizeof(*ll
), GFP_KERNEL
);
143 skb_queue_head_init(&ll
->txq
);
144 skb_queue_head_init(&ll
->tx_wait_q
);
145 spin_lock_init(&ll
->hcill_lock
);
147 ll
->hcill_state
= HCILL_AWAKE
;
152 struct ll_device
*lldev
= serdev_device_get_drvdata(hu
->serdev
);
153 serdev_device_open(hu
->serdev
);
154 if (!IS_ERR(lldev
->ext_clk
))
155 clk_prepare_enable(lldev
->ext_clk
);
161 /* Flush protocol data */
162 static int ll_flush(struct hci_uart
*hu
)
164 struct ll_struct
*ll
= hu
->priv
;
168 skb_queue_purge(&ll
->tx_wait_q
);
169 skb_queue_purge(&ll
->txq
);
175 static int ll_close(struct hci_uart
*hu
)
177 struct ll_struct
*ll
= hu
->priv
;
181 skb_queue_purge(&ll
->tx_wait_q
);
182 skb_queue_purge(&ll
->txq
);
184 kfree_skb(ll
->rx_skb
);
187 struct ll_device
*lldev
= serdev_device_get_drvdata(hu
->serdev
);
188 gpiod_set_value_cansleep(lldev
->enable_gpio
, 0);
190 clk_disable_unprepare(lldev
->ext_clk
);
192 serdev_device_close(hu
->serdev
);
203 * internal function, which does common work of the device wake up process:
204 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
205 * 2. changes internal state to HCILL_AWAKE.
206 * Note: assumes that hcill_lock spinlock is taken,
207 * shouldn't be called otherwise!
209 static void __ll_do_awake(struct ll_struct
*ll
)
211 struct sk_buff
*skb
= NULL
;
213 while ((skb
= skb_dequeue(&ll
->tx_wait_q
)))
214 skb_queue_tail(&ll
->txq
, skb
);
216 ll
->hcill_state
= HCILL_AWAKE
;
220 * Called upon a wake-up-indication from the device
222 static void ll_device_want_to_wakeup(struct hci_uart
*hu
)
225 struct ll_struct
*ll
= hu
->priv
;
229 /* lock hcill state */
230 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
232 switch (ll
->hcill_state
) {
233 case HCILL_ASLEEP_TO_AWAKE
:
235 * This state means that both the host and the BRF chip
236 * have simultaneously sent a wake-up-indication packet.
237 * Traditionally, in this case, receiving a wake-up-indication
238 * was enough and an additional wake-up-ack wasn't needed.
239 * This has changed with the BRF6350, which does require an
240 * explicit wake-up-ack. Other BRF versions, which do not
241 * require an explicit ack here, do accept it, thus it is
242 * perfectly safe to always send one.
244 BT_DBG("dual wake-up-indication");
245 /* deliberate fall-through - do not add break */
247 /* acknowledge device wake up */
248 if (send_hcill_cmd(HCILL_WAKE_UP_ACK
, hu
) < 0) {
249 BT_ERR("cannot acknowledge device wake up");
254 /* any other state is illegal */
255 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll
->hcill_state
);
259 /* send pending packets and change state to HCILL_AWAKE */
263 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
265 /* actually send the packets */
266 hci_uart_tx_wakeup(hu
);
270 * Called upon a sleep-indication from the device
272 static void ll_device_want_to_sleep(struct hci_uart
*hu
)
275 struct ll_struct
*ll
= hu
->priv
;
279 /* lock hcill state */
280 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
283 if (ll
->hcill_state
!= HCILL_AWAKE
)
284 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll
->hcill_state
);
286 /* acknowledge device sleep */
287 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK
, hu
) < 0) {
288 BT_ERR("cannot acknowledge device sleep");
293 ll
->hcill_state
= HCILL_ASLEEP
;
296 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
298 /* actually send the sleep ack packet */
299 hci_uart_tx_wakeup(hu
);
303 * Called upon wake-up-acknowledgement from the device
305 static void ll_device_woke_up(struct hci_uart
*hu
)
308 struct ll_struct
*ll
= hu
->priv
;
312 /* lock hcill state */
313 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
316 if (ll
->hcill_state
!= HCILL_ASLEEP_TO_AWAKE
)
317 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll
->hcill_state
);
319 /* send pending packets and change state to HCILL_AWAKE */
322 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
324 /* actually send the packets */
325 hci_uart_tx_wakeup(hu
);
328 /* Enqueue frame for transmittion (padding, crc, etc) */
329 /* may be called from two simultaneous tasklets */
330 static int ll_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
332 unsigned long flags
= 0;
333 struct ll_struct
*ll
= hu
->priv
;
335 BT_DBG("hu %p skb %p", hu
, skb
);
337 /* Prepend skb with frame type */
338 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
340 /* lock hcill state */
341 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
343 /* act according to current state */
344 switch (ll
->hcill_state
) {
346 BT_DBG("device awake, sending normally");
347 skb_queue_tail(&ll
->txq
, skb
);
350 BT_DBG("device asleep, waking up and queueing packet");
351 /* save packet for later */
352 skb_queue_tail(&ll
->tx_wait_q
, skb
);
354 if (send_hcill_cmd(HCILL_WAKE_UP_IND
, hu
) < 0) {
355 BT_ERR("cannot wake up device");
358 ll
->hcill_state
= HCILL_ASLEEP_TO_AWAKE
;
360 case HCILL_ASLEEP_TO_AWAKE
:
361 BT_DBG("device waking up, queueing packet");
362 /* transient state; just keep packet for later */
363 skb_queue_tail(&ll
->tx_wait_q
, skb
);
366 BT_ERR("illegal hcill state: %ld (losing packet)", ll
->hcill_state
);
371 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
376 static inline int ll_check_data_len(struct hci_dev
*hdev
, struct ll_struct
*ll
, int len
)
378 int room
= skb_tailroom(ll
->rx_skb
);
380 BT_DBG("len %d room %d", len
, room
);
383 hci_recv_frame(hdev
, ll
->rx_skb
);
384 } else if (len
> room
) {
385 BT_ERR("Data length is too large");
386 kfree_skb(ll
->rx_skb
);
388 ll
->rx_state
= HCILL_W4_DATA
;
393 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
401 static int ll_recv(struct hci_uart
*hu
, const void *data
, int count
)
403 struct ll_struct
*ll
= hu
->priv
;
405 struct hci_event_hdr
*eh
;
406 struct hci_acl_hdr
*ah
;
407 struct hci_sco_hdr
*sh
;
410 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu
, count
, ll
->rx_state
, ll
->rx_count
);
415 len
= min_t(unsigned int, ll
->rx_count
, count
);
416 skb_put_data(ll
->rx_skb
, ptr
, len
);
417 ll
->rx_count
-= len
; count
-= len
; ptr
+= len
;
422 switch (ll
->rx_state
) {
424 BT_DBG("Complete data");
425 hci_recv_frame(hu
->hdev
, ll
->rx_skb
);
427 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
431 case HCILL_W4_EVENT_HDR
:
432 eh
= hci_event_hdr(ll
->rx_skb
);
434 BT_DBG("Event header: evt 0x%2.2x plen %d", eh
->evt
, eh
->plen
);
436 ll_check_data_len(hu
->hdev
, ll
, eh
->plen
);
439 case HCILL_W4_ACL_HDR
:
440 ah
= hci_acl_hdr(ll
->rx_skb
);
441 dlen
= __le16_to_cpu(ah
->dlen
);
443 BT_DBG("ACL header: dlen %d", dlen
);
445 ll_check_data_len(hu
->hdev
, ll
, dlen
);
448 case HCILL_W4_SCO_HDR
:
449 sh
= hci_sco_hdr(ll
->rx_skb
);
451 BT_DBG("SCO header: dlen %d", sh
->dlen
);
453 ll_check_data_len(hu
->hdev
, ll
, sh
->dlen
);
458 /* HCILL_W4_PACKET_TYPE */
461 BT_DBG("Event packet");
462 ll
->rx_state
= HCILL_W4_EVENT_HDR
;
463 ll
->rx_count
= HCI_EVENT_HDR_SIZE
;
464 type
= HCI_EVENT_PKT
;
467 case HCI_ACLDATA_PKT
:
468 BT_DBG("ACL packet");
469 ll
->rx_state
= HCILL_W4_ACL_HDR
;
470 ll
->rx_count
= HCI_ACL_HDR_SIZE
;
471 type
= HCI_ACLDATA_PKT
;
474 case HCI_SCODATA_PKT
:
475 BT_DBG("SCO packet");
476 ll
->rx_state
= HCILL_W4_SCO_HDR
;
477 ll
->rx_count
= HCI_SCO_HDR_SIZE
;
478 type
= HCI_SCODATA_PKT
;
482 case HCILL_GO_TO_SLEEP_IND
:
483 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
484 ll_device_want_to_sleep(hu
);
488 case HCILL_GO_TO_SLEEP_ACK
:
489 /* shouldn't happen */
490 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll
->hcill_state
);
494 case HCILL_WAKE_UP_IND
:
495 BT_DBG("HCILL_WAKE_UP_IND packet");
496 ll_device_want_to_wakeup(hu
);
500 case HCILL_WAKE_UP_ACK
:
501 BT_DBG("HCILL_WAKE_UP_ACK packet");
502 ll_device_woke_up(hu
);
507 BT_ERR("Unknown HCI packet type %2.2x", (__u8
)*ptr
);
508 hu
->hdev
->stat
.err_rx
++;
515 /* Allocate packet */
516 ll
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
518 BT_ERR("Can't allocate mem for new packet");
519 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
524 hci_skb_pkt_type(ll
->rx_skb
) = type
;
530 static struct sk_buff
*ll_dequeue(struct hci_uart
*hu
)
532 struct ll_struct
*ll
= hu
->priv
;
533 return skb_dequeue(&ll
->txq
);
536 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
537 static int read_local_version(struct hci_dev
*hdev
)
540 unsigned short version
= 0;
542 struct hci_rp_read_local_version
*ver
;
544 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
, HCI_INIT_TIMEOUT
);
546 bt_dev_err(hdev
, "Reading TI version information failed (%ld)",
550 if (skb
->len
!= sizeof(*ver
)) {
555 ver
= (struct hci_rp_read_local_version
*)skb
->data
;
556 if (le16_to_cpu(ver
->manufacturer
) != 13) {
561 version
= le16_to_cpu(ver
->lmp_subver
);
564 if (err
) bt_dev_err(hdev
, "Failed to read TI version info: %d", err
);
566 return err
? err
: version
;
570 * download_firmware -
571 * internal function which parses through the .bts firmware
572 * script file intreprets SEND, DELAY actions only as of now
574 static int download_firmware(struct ll_device
*lldev
)
576 unsigned short chip
, min_ver
, maj_ver
;
577 int version
, err
, len
;
578 unsigned char *ptr
, *action_ptr
;
579 unsigned char bts_scr_name
[40]; /* 40 char long bts scr name? */
580 const struct firmware
*fw
;
582 struct hci_command
*cmd
;
584 version
= read_local_version(lldev
->hu
.hdev
);
588 chip
= (version
& 0x7C00) >> 10;
589 min_ver
= (version
& 0x007F);
590 maj_ver
= (version
& 0x0380) >> 7;
591 if (version
& 0x8000)
594 snprintf(bts_scr_name
, sizeof(bts_scr_name
),
595 "ti-connectivity/TIInit_%d.%d.%d.bts",
596 chip
, maj_ver
, min_ver
);
598 err
= request_firmware(&fw
, bts_scr_name
, &lldev
->serdev
->dev
);
599 if (err
|| !fw
->data
|| !fw
->size
) {
600 bt_dev_err(lldev
->hu
.hdev
, "request_firmware failed(errno %d) for %s",
604 ptr
= (void *)fw
->data
;
606 /* bts_header to remove out magic number and
609 ptr
+= sizeof(struct bts_header
);
610 len
-= sizeof(struct bts_header
);
612 while (len
> 0 && ptr
) {
613 bt_dev_dbg(lldev
->hu
.hdev
, " action size %d, type %d ",
614 ((struct bts_action
*)ptr
)->size
,
615 ((struct bts_action
*)ptr
)->type
);
617 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
619 switch (((struct bts_action
*)ptr
)->type
) {
620 case ACTION_SEND_COMMAND
: /* action send */
621 bt_dev_dbg(lldev
->hu
.hdev
, "S");
622 cmd
= (struct hci_command
*)action_ptr
;
623 if (cmd
->opcode
== 0xff36) {
624 /* ignore remote change
625 * baud rate HCI VS command */
626 bt_dev_warn(lldev
->hu
.hdev
, "change remote baud rate command in firmware");
629 if (cmd
->prefix
!= 1)
630 bt_dev_dbg(lldev
->hu
.hdev
, "command type %d\n", cmd
->prefix
);
632 skb
= __hci_cmd_sync(lldev
->hu
.hdev
, cmd
->opcode
, cmd
->plen
, &cmd
->speed
, HCI_INIT_TIMEOUT
);
634 bt_dev_err(lldev
->hu
.hdev
, "send command failed\n");
640 case ACTION_WAIT_EVENT
: /* wait */
641 /* no need to wait as command was synchronous */
642 bt_dev_dbg(lldev
->hu
.hdev
, "W");
644 case ACTION_DELAY
: /* sleep */
645 bt_dev_info(lldev
->hu
.hdev
, "sleep command in scr");
646 mdelay(((struct bts_action_delay
*)action_ptr
)->msec
);
649 len
-= (sizeof(struct bts_action
) +
650 ((struct bts_action
*)ptr
)->size
);
651 ptr
+= sizeof(struct bts_action
) +
652 ((struct bts_action
*)ptr
)->size
;
656 /* fw download complete */
657 release_firmware(fw
);
661 static int ll_setup(struct hci_uart
*hu
)
664 struct ll_device
*lldev
;
665 struct serdev_device
*serdev
= hu
->serdev
;
671 lldev
= serdev_device_get_drvdata(serdev
);
673 serdev_device_set_flow_control(serdev
, true);
676 /* Configure BT_EN to HIGH state */
677 gpiod_set_value_cansleep(lldev
->enable_gpio
, 0);
679 gpiod_set_value_cansleep(lldev
->enable_gpio
, 1);
682 err
= download_firmware(lldev
);
686 /* Toggle BT_EN and retry */
687 bt_dev_err(hu
->hdev
, "download firmware failed, retrying...");
693 /* Operational speed if any */
695 speed
= hu
->oper_speed
;
696 else if (hu
->proto
->oper_speed
)
697 speed
= hu
->proto
->oper_speed
;
702 struct sk_buff
*skb
= __hci_cmd_sync(hu
->hdev
, 0xff36, sizeof(speed
), &speed
, HCI_INIT_TIMEOUT
);
705 serdev_device_set_baudrate(serdev
, speed
);
712 static const struct hci_uart_proto llp
;
714 static int hci_ti_probe(struct serdev_device
*serdev
)
717 struct ll_device
*lldev
;
718 u32 max_speed
= 3000000;
720 lldev
= devm_kzalloc(&serdev
->dev
, sizeof(struct ll_device
), GFP_KERNEL
);
725 serdev_device_set_drvdata(serdev
, lldev
);
726 lldev
->serdev
= hu
->serdev
= serdev
;
728 lldev
->enable_gpio
= devm_gpiod_get_optional(&serdev
->dev
, "enable", GPIOD_OUT_LOW
);
729 if (IS_ERR(lldev
->enable_gpio
))
730 return PTR_ERR(lldev
->enable_gpio
);
732 lldev
->ext_clk
= devm_clk_get(&serdev
->dev
, "ext_clock");
733 if (IS_ERR(lldev
->ext_clk
) && PTR_ERR(lldev
->ext_clk
) != -ENOENT
)
734 return PTR_ERR(lldev
->ext_clk
);
736 of_property_read_u32(serdev
->dev
.of_node
, "max-speed", &max_speed
);
737 hci_uart_set_speeds(hu
, 115200, max_speed
);
739 return hci_uart_register_device(hu
, &llp
);
742 static void hci_ti_remove(struct serdev_device
*serdev
)
744 struct ll_device
*lldev
= serdev_device_get_drvdata(serdev
);
745 struct hci_uart
*hu
= &lldev
->hu
;
746 struct hci_dev
*hdev
= hu
->hdev
;
748 cancel_work_sync(&hu
->write_work
);
750 hci_unregister_dev(hdev
);
752 hu
->proto
->close(hu
);
755 static const struct of_device_id hci_ti_of_match
[] = {
756 { .compatible
= "ti,wl1271-st" },
757 { .compatible
= "ti,wl1273-st" },
758 { .compatible
= "ti,wl1281-st" },
759 { .compatible
= "ti,wl1283-st" },
760 { .compatible
= "ti,wl1285-st" },
761 { .compatible
= "ti,wl1801-st" },
762 { .compatible
= "ti,wl1805-st" },
763 { .compatible
= "ti,wl1807-st" },
764 { .compatible
= "ti,wl1831-st" },
765 { .compatible
= "ti,wl1835-st" },
766 { .compatible
= "ti,wl1837-st" },
769 MODULE_DEVICE_TABLE(of
, hci_ti_of_match
);
771 static struct serdev_device_driver hci_ti_drv
= {
774 .of_match_table
= of_match_ptr(hci_ti_of_match
),
776 .probe
= hci_ti_probe
,
777 .remove
= hci_ti_remove
,
780 #define ll_setup NULL
783 static const struct hci_uart_proto llp
= {
790 .enqueue
= ll_enqueue
,
791 .dequeue
= ll_dequeue
,
795 int __init
ll_init(void)
797 serdev_device_driver_register(&hci_ti_drv
);
799 return hci_uart_register_proto(&llp
);
802 int __exit
ll_deinit(void)
804 serdev_device_driver_unregister(&hci_ti_drv
);
806 return hci_uart_unregister_proto(&llp
);