1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth HCI UART driver
6 * Copyright (C) 2002-2003 Fabrizio Gennari <fabrizio.gennari@philips.com>
7 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
10 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/interrupt.h>
17 #include <linux/ptrace.h>
18 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/signal.h>
25 #include <linux/ioctl.h>
26 #include <linux/skbuff.h>
27 #include <linux/bitrev.h>
28 #include <asm/unaligned.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
35 static bool txcrc = true;
36 static bool hciextn = true;
38 #define BCSP_TXWINSIZE 4
40 #define BCSP_ACK_PKT 0x05
41 #define BCSP_LE_PKT 0x06
44 struct sk_buff_head unack; /* Unack'ed packets queue */
45 struct sk_buff_head rel; /* Reliable packets queue */
46 struct sk_buff_head unrel; /* Unreliable packets queue */
48 unsigned long rx_count;
49 struct sk_buff *rx_skb;
50 u8 rxseq_txack; /* rxseq == txack. */
51 u8 rxack; /* Last packet sent by us that the peer ack'ed */
52 struct timer_list tbcsp;
56 BCSP_W4_PKT_DELIMITER,
70 u8 txack_req; /* Do we need to send ack's to the peer? */
72 /* Reliable packet sequence number - used to assign seq to each rel pkt. */
76 /* ---- BCSP CRC calculation ---- */
78 /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
79 * initial value 0xffff, bits shifted in reverse order.
82 static const u16 crc_table[] = {
83 0x0000, 0x1081, 0x2102, 0x3183,
84 0x4204, 0x5285, 0x6306, 0x7387,
85 0x8408, 0x9489, 0xa50a, 0xb58b,
86 0xc60c, 0xd68d, 0xe70e, 0xf78f
89 /* Initialise the crc calculator */
90 #define BCSP_CRC_INIT(x) x = 0xffff
92 /* Update crc with next data byte
95 * The data byte is treated as two nibbles. The crc is generated
96 * in reverse, i.e., bits are fed into the register from the top.
98 static void bcsp_crc_update(u16 *crc, u8 d)
102 reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
103 reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
108 /* ---- BCSP core ---- */
110 static void bcsp_slip_msgdelim(struct sk_buff *skb)
112 const char pkt_delim = 0xc0;
114 skb_put_data(skb, &pkt_delim, 1);
117 static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
119 const char esc_c0[2] = { 0xdb, 0xdc };
120 const char esc_db[2] = { 0xdb, 0xdd };
124 skb_put_data(skb, &esc_c0, 2);
127 skb_put_data(skb, &esc_db, 2);
130 skb_put_data(skb, &c, 1);
134 static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
136 struct bcsp_struct *bcsp = hu->priv;
138 if (skb->len > 0xFFF) {
139 BT_ERR("Packet too long");
144 switch (hci_skb_pkt_type(skb)) {
145 case HCI_ACLDATA_PKT:
146 case HCI_COMMAND_PKT:
147 skb_queue_tail(&bcsp->rel, skb);
150 case HCI_SCODATA_PKT:
151 skb_queue_tail(&bcsp->unrel, skb);
155 BT_ERR("Unknown packet type");
163 static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
164 int len, int pkt_type)
166 struct sk_buff *nskb;
168 u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
172 case HCI_ACLDATA_PKT:
173 chan = 6; /* BCSP ACL channel */
174 rel = 1; /* reliable channel */
176 case HCI_COMMAND_PKT:
177 chan = 5; /* BCSP cmd/evt channel */
178 rel = 1; /* reliable channel */
180 case HCI_SCODATA_PKT:
181 chan = 7; /* BCSP SCO channel */
182 rel = 0; /* unreliable channel */
185 chan = 1; /* BCSP LE channel */
186 rel = 0; /* unreliable channel */
189 chan = 0; /* BCSP internal channel */
190 rel = 0; /* unreliable channel */
193 BT_ERR("Unknown packet type");
197 if (hciextn && chan == 5) {
198 __le16 opcode = ((struct hci_command_hdr *)data)->opcode;
200 /* Vendor specific commands */
201 if (hci_opcode_ogf(__le16_to_cpu(opcode)) == 0x3f) {
202 u8 desc = *(data + HCI_COMMAND_HDR_SIZE);
204 if ((desc & 0xf0) == 0xc0) {
205 data += HCI_COMMAND_HDR_SIZE + 1;
206 len -= HCI_COMMAND_HDR_SIZE + 1;
212 /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
213 * (because bytes 0xc0 and 0xdb are escaped, worst case is
214 * when the packet is all made of 0xc0 and 0xdb :) )
215 * + 2 (0xc0 delimiters at start and end).
218 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
222 hci_skb_pkt_type(nskb) = pkt_type;
224 bcsp_slip_msgdelim(nskb);
226 hdr[0] = bcsp->rxseq_txack << 3;
228 BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
231 hdr[0] |= 0x80 + bcsp->msgq_txseq;
232 BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
233 bcsp->msgq_txseq = (bcsp->msgq_txseq + 1) & 0x07;
239 hdr[1] = ((len << 4) & 0xff) | chan;
241 hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
243 /* Put BCSP header */
244 for (i = 0; i < 4; i++) {
245 bcsp_slip_one_byte(nskb, hdr[i]);
248 bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
252 for (i = 0; i < len; i++) {
253 bcsp_slip_one_byte(nskb, data[i]);
256 bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
261 bcsp_txmsg_crc = bitrev16(bcsp_txmsg_crc);
262 bcsp_slip_one_byte(nskb, (u8)((bcsp_txmsg_crc >> 8) & 0x00ff));
263 bcsp_slip_one_byte(nskb, (u8)(bcsp_txmsg_crc & 0x00ff));
266 bcsp_slip_msgdelim(nskb);
270 /* This is a rewrite of pkt_avail in ABCSP */
271 static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
273 struct bcsp_struct *bcsp = hu->priv;
277 /* First of all, check for unreliable messages in the queue,
278 * since they have priority
281 skb = skb_dequeue(&bcsp->unrel);
283 struct sk_buff *nskb;
285 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
286 hci_skb_pkt_type(skb));
291 skb_queue_head(&bcsp->unrel, skb);
292 BT_ERR("Could not dequeue pkt because alloc_skb failed");
296 /* Now, try to send a reliable pkt. We can only send a
297 * reliable packet if the number of packets sent but not yet ack'ed
298 * is < than the winsize
301 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
303 if (bcsp->unack.qlen < BCSP_TXWINSIZE) {
304 skb = skb_dequeue(&bcsp->rel);
306 struct sk_buff *nskb;
308 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
309 hci_skb_pkt_type(skb));
311 __skb_queue_tail(&bcsp->unack, skb);
312 mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
313 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
316 skb_queue_head(&bcsp->rel, skb);
317 BT_ERR("Could not dequeue pkt because alloc_skb failed");
322 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
324 /* We could not send a reliable packet, either because there are
325 * none or because there are too many unack'ed pkts. Did we receive
326 * any packets we have not acknowledged yet ?
329 if (bcsp->txack_req) {
330 /* if so, craft an empty ACK pkt and send it on BCSP unreliable
333 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
337 /* We have nothing to send */
341 static int bcsp_flush(struct hci_uart *hu)
347 /* Remove ack'ed packets */
348 static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
350 struct sk_buff *skb, *tmp;
352 int i, pkts_to_be_removed;
355 spin_lock_irqsave(&bcsp->unack.lock, flags);
357 pkts_to_be_removed = skb_queue_len(&bcsp->unack);
358 seqno = bcsp->msgq_txseq;
360 while (pkts_to_be_removed) {
361 if (bcsp->rxack == seqno)
363 pkts_to_be_removed--;
364 seqno = (seqno - 1) & 0x07;
367 if (bcsp->rxack != seqno)
368 BT_ERR("Peer acked invalid packet");
370 BT_DBG("Removing %u pkts out of %u, up to seqno %u",
371 pkts_to_be_removed, skb_queue_len(&bcsp->unack),
375 skb_queue_walk_safe(&bcsp->unack, skb, tmp) {
376 if (i >= pkts_to_be_removed)
380 __skb_unlink(skb, &bcsp->unack);
384 if (skb_queue_empty(&bcsp->unack))
385 del_timer(&bcsp->tbcsp);
387 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
389 if (i != pkts_to_be_removed)
390 BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
393 /* Handle BCSP link-establishment packets. When we
394 * detect a "sync" packet, symptom that the BT module has reset,
395 * we do nothing :) (yet)
397 static void bcsp_handle_le_pkt(struct hci_uart *hu)
399 struct bcsp_struct *bcsp = hu->priv;
400 u8 conf_pkt[4] = { 0xad, 0xef, 0xac, 0xed };
401 u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
402 u8 sync_pkt[4] = { 0xda, 0xdc, 0xed, 0xed };
404 /* spot "conf" pkts and reply with a "conf rsp" pkt */
405 if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
406 !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
407 struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
409 BT_DBG("Found a LE conf pkt");
412 skb_put_data(nskb, conf_rsp_pkt, 4);
413 hci_skb_pkt_type(nskb) = BCSP_LE_PKT;
415 skb_queue_head(&bcsp->unrel, nskb);
416 hci_uart_tx_wakeup(hu);
418 /* Spot "sync" pkts. If we find one...disaster! */
419 else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
420 !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
421 BT_ERR("Found a LE sync pkt, card has reset");
425 static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
427 const u8 c0 = 0xc0, db = 0xdb;
429 switch (bcsp->rx_esc_state) {
430 case BCSP_ESCSTATE_NOESC:
433 bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
436 skb_put_data(bcsp->rx_skb, &byte, 1);
437 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
438 bcsp->rx_state != BCSP_W4_CRC)
439 bcsp_crc_update(&bcsp->message_crc, byte);
444 case BCSP_ESCSTATE_ESC:
447 skb_put_data(bcsp->rx_skb, &c0, 1);
448 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
449 bcsp->rx_state != BCSP_W4_CRC)
450 bcsp_crc_update(&bcsp->message_crc, 0xc0);
451 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
456 skb_put_data(bcsp->rx_skb, &db, 1);
457 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
458 bcsp->rx_state != BCSP_W4_CRC)
459 bcsp_crc_update(&bcsp->message_crc, 0xdb);
460 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
465 BT_ERR("Invalid byte %02x after esc byte", byte);
466 kfree_skb(bcsp->rx_skb);
468 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
474 static void bcsp_complete_rx_pkt(struct hci_uart *hu)
476 struct bcsp_struct *bcsp = hu->priv;
479 if (bcsp->rx_skb->data[0] & 0x80) { /* reliable pkt */
480 BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
482 /* check the rx sequence number is as expected */
483 if ((bcsp->rx_skb->data[0] & 0x07) == bcsp->rxseq_txack) {
485 bcsp->rxseq_txack %= 0x8;
487 /* handle re-transmitted packet or
488 * when packet was missed
490 BT_ERR("Out-of-order packet arrived, got %u expected %u",
491 bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
493 /* do not process out-of-order packet payload */
497 /* send current txack value to all received reliable packets */
500 /* If needed, transmit an ack pkt */
501 hci_uart_tx_wakeup(hu);
504 bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
505 BT_DBG("Request for pkt %u from card", bcsp->rxack);
507 /* handle received ACK indications,
508 * including those from out-of-order packets
513 if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
514 (bcsp->rx_skb->data[0] & 0x80)) {
515 hci_skb_pkt_type(bcsp->rx_skb) = HCI_ACLDATA_PKT;
517 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
518 (bcsp->rx_skb->data[0] & 0x80)) {
519 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
521 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
522 hci_skb_pkt_type(bcsp->rx_skb) = HCI_SCODATA_PKT;
524 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
525 !(bcsp->rx_skb->data[0] & 0x80)) {
526 bcsp_handle_le_pkt(hu);
534 struct hci_event_hdr hdr;
535 u8 desc = (bcsp->rx_skb->data[1] & 0x0f);
537 if (desc != 0 && desc != 1) {
540 skb_pull(bcsp->rx_skb, 4);
541 memcpy(skb_push(bcsp->rx_skb, 1), &desc, 1);
544 hdr.plen = bcsp->rx_skb->len;
545 memcpy(skb_push(bcsp->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE);
546 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
548 hci_recv_frame(hu->hdev, bcsp->rx_skb);
550 BT_ERR("Packet for unknown channel (%u %s)",
551 bcsp->rx_skb->data[1] & 0x0f,
552 bcsp->rx_skb->data[0] & 0x80 ?
553 "reliable" : "unreliable");
554 kfree_skb(bcsp->rx_skb);
557 kfree_skb(bcsp->rx_skb);
558 } else if (pass_up == 1) {
559 /* Pull out BCSP hdr */
560 skb_pull(bcsp->rx_skb, 4);
562 hci_recv_frame(hu->hdev, bcsp->rx_skb);
564 /* ignore packet payload of already ACKed re-transmitted
565 * packets or when a packet was missed in the BCSP window
567 kfree_skb(bcsp->rx_skb);
570 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
574 static u16 bscp_get_crc(struct bcsp_struct *bcsp)
576 return get_unaligned_be16(&bcsp->rx_skb->data[bcsp->rx_skb->len - 2]);
580 static int bcsp_recv(struct hci_uart *hu, const void *data, int count)
582 struct bcsp_struct *bcsp = hu->priv;
583 const unsigned char *ptr;
585 BT_DBG("hu %p count %d rx_state %d rx_count %ld",
586 hu, count, bcsp->rx_state, bcsp->rx_count);
590 if (bcsp->rx_count) {
592 BT_ERR("Short BCSP packet");
593 kfree_skb(bcsp->rx_skb);
595 bcsp->rx_state = BCSP_W4_PKT_START;
598 bcsp_unslip_one_byte(bcsp, *ptr);
604 switch (bcsp->rx_state) {
605 case BCSP_W4_BCSP_HDR:
606 if ((0xff & (u8)~(bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
607 bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
608 BT_ERR("Error in BCSP hdr checksum");
609 kfree_skb(bcsp->rx_skb);
611 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
615 bcsp->rx_state = BCSP_W4_DATA;
616 bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) +
617 (bcsp->rx_skb->data[2] << 4); /* May be 0 */
621 if (bcsp->rx_skb->data[0] & 0x40) { /* pkt with crc */
622 bcsp->rx_state = BCSP_W4_CRC;
625 bcsp_complete_rx_pkt(hu);
629 if (bitrev16(bcsp->message_crc) != bscp_get_crc(bcsp)) {
630 BT_ERR("Checksum failed: computed %04x received %04x",
631 bitrev16(bcsp->message_crc),
634 kfree_skb(bcsp->rx_skb);
636 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
640 skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
641 bcsp_complete_rx_pkt(hu);
644 case BCSP_W4_PKT_DELIMITER:
647 bcsp->rx_state = BCSP_W4_PKT_START;
650 /*BT_ERR("Ignoring byte %02x", *ptr);*/
656 case BCSP_W4_PKT_START:
663 bcsp->rx_state = BCSP_W4_BCSP_HDR;
665 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
666 BCSP_CRC_INIT(bcsp->message_crc);
668 /* Do not increment ptr or decrement count
669 * Allocate packet. Max len of a BCSP pkt=
670 * 0xFFF (payload) +4 (header) +2 (crc)
673 bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
675 BT_ERR("Can't allocate mem for new packet");
676 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
688 /* Arrange to retransmit all messages in the relq. */
689 static void bcsp_timed_event(struct timer_list *t)
691 struct bcsp_struct *bcsp = from_timer(bcsp, t, tbcsp);
692 struct hci_uart *hu = bcsp->hu;
696 BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen);
698 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
700 while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
701 bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
702 skb_queue_head(&bcsp->rel, skb);
705 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
707 hci_uart_tx_wakeup(hu);
710 static int bcsp_open(struct hci_uart *hu)
712 struct bcsp_struct *bcsp;
716 bcsp = kzalloc(sizeof(*bcsp), GFP_KERNEL);
722 skb_queue_head_init(&bcsp->unack);
723 skb_queue_head_init(&bcsp->rel);
724 skb_queue_head_init(&bcsp->unrel);
726 timer_setup(&bcsp->tbcsp, bcsp_timed_event, 0);
728 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
736 static int bcsp_close(struct hci_uart *hu)
738 struct bcsp_struct *bcsp = hu->priv;
740 del_timer_sync(&bcsp->tbcsp);
746 skb_queue_purge(&bcsp->unack);
747 skb_queue_purge(&bcsp->rel);
748 skb_queue_purge(&bcsp->unrel);
751 kfree_skb(bcsp->rx_skb);
759 static const struct hci_uart_proto bcsp = {
764 .enqueue = bcsp_enqueue,
765 .dequeue = bcsp_dequeue,
770 int __init bcsp_init(void)
772 return hci_uart_register_proto(&bcsp);
775 int __exit bcsp_deinit(void)
777 return hci_uart_unregister_proto(&bcsp);
780 module_param(txcrc, bool, 0644);
781 MODULE_PARM_DESC(txcrc, "Transmit CRC with every BCSP packet");
783 module_param(hciextn, bool, 0644);
784 MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into BCSP packets");