25 #include <linux/module.h>
27 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/fcntl.h>
32 #include <linux/ptrace.h>
33 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/errno.h>
38 #include <linux/string.h>
39 #include <linux/signal.h>
43 #include <asm/unaligned.h>
52 static bool txcrc = 1;
53 static bool hciextn = 1;
55 #define BCSP_TXWINSIZE 4
57 #define BCSP_ACK_PKT 0x05
58 #define BCSP_LE_PKT 0x06
97 static const u16 crc_table[] = {
98 0x0000, 0x1081, 0x2102, 0x3183,
99 0x4204, 0x5285, 0x6306, 0x7387,
100 0x8408, 0x9489, 0xa50a, 0xb58b,
101 0xc60c, 0xd68d, 0xe70e, 0xf78f
105 #define BCSP_CRC_INIT(x) x = 0xffff
114 static void bcsp_crc_update(
u16 *
crc,
u8 d)
118 reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
119 reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
126 static void bcsp_slip_msgdelim(
struct sk_buff *
skb)
128 const char pkt_delim = 0xc0;
133 static void bcsp_slip_one_byte(
struct sk_buff *skb,
u8 c)
135 const char esc_c0[2] = { 0xdb, 0xdc };
136 const char esc_db[2] = { 0xdb, 0xdd };
154 if (skb->
len > 0xFFF) {
155 BT_ERR(
"Packet too long");
160 switch (
bt_cb(skb)->pkt_type) {
171 BT_ERR(
"Unknown packet type");
209 BT_ERR(
"Unknown packet type");
213 if (hciextn && chan == 5) {
219 if ((desc & 0xf0) == 0xc0) {
232 nskb = alloc_skb((len + 6) * 2 + 2,
GFP_ATOMIC);
236 bt_cb(nskb)->pkt_type = pkt_type;
238 bcsp_slip_msgdelim(nskb);
253 hdr[1] = ((len << 4) & 0xff) | chan;
255 hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
258 for (i = 0; i < 4; i++) {
259 bcsp_slip_one_byte(nskb, hdr[i]);
262 bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
266 for (i = 0; i < len; i++) {
267 bcsp_slip_one_byte(nskb, data[i]);
270 bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
275 bcsp_txmsg_crc =
bitrev16(bcsp_txmsg_crc);
276 bcsp_slip_one_byte(nskb, (
u8) ((bcsp_txmsg_crc >> 8) & 0x00ff));
277 bcsp_slip_one_byte(nskb, (
u8) (bcsp_txmsg_crc & 0x00ff));
280 bcsp_slip_msgdelim(nskb);
301 BT_ERR(
"Could not dequeue pkt because alloc_skb failed");
314 __skb_queue_tail(&bcsp->
unack, skb);
316 spin_unlock_irqrestore(&bcsp->
unack.lock, flags);
320 BT_ERR(
"Could not dequeue pkt because alloc_skb failed");
324 spin_unlock_irqrestore(&bcsp->
unack.lock, flags);
341 static int bcsp_flush(
struct hci_uart *hu)
348 static void bcsp_pkt_cull(
struct bcsp_struct *bcsp)
352 int i, pkts_to_be_removed;
357 pkts_to_be_removed = skb_queue_len(&bcsp->
unack);
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))
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);
396 static void bcsp_handle_le_pkt(
struct hci_uart *hu)
399 u8 conf_pkt[4] = { 0xad, 0xef, 0xac, 0xed };
400 u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
401 u8 sync_pkt[4] = { 0xda, 0xdc, 0xed, 0xed };
404 if (bcsp->
rx_skb->data[1] >> 4 == 4 && bcsp->
rx_skb->data[2] == 0 &&
408 BT_DBG(
"Found a LE conf pkt");
418 else if (bcsp->
rx_skb->data[1] >> 4 == 4 && bcsp->
rx_skb->data[2] == 0 &&
420 BT_ERR(
"Found a LE sync pkt, card has reset");
424 static inline void bcsp_unslip_one_byte(
struct bcsp_struct *bcsp,
unsigned char byte)
426 const u8 c0 = 0xc0, db = 0xdb;
429 case BCSP_ESCSTATE_NOESC:
436 if ((bcsp->
rx_skb-> data[0] & 0x40) != 0 &&
443 case BCSP_ESCSTATE_ESC:
447 if ((bcsp->
rx_skb-> data[0] & 0x40) != 0 &&
449 bcsp_crc_update(&bcsp-> message_crc, 0xc0);
456 if ((bcsp->
rx_skb-> data[0] & 0x40) != 0 &&
458 bcsp_crc_update(&bcsp-> message_crc, 0xdb);
464 BT_ERR (
"Invalid byte %02x after esc byte", byte);
467 bcsp->
rx_state = BCSP_W4_PKT_DELIMITER;
473 static void bcsp_complete_rx_pkt(
struct hci_uart *hu)
478 if (bcsp->
rx_skb->data[0] & 0x80) {
489 BT_DBG(
"Request for pkt %u from card", bcsp->
rxack);
492 if ((bcsp->
rx_skb->data[1] & 0x0f) == 6 &&
493 bcsp->
rx_skb->data[0] & 0x80) {
496 }
else if ((bcsp->
rx_skb->data[1] & 0x0f) == 5 &&
497 bcsp->
rx_skb->data[0] & 0x80) {
500 }
else if ((bcsp->
rx_skb->data[1] & 0x0f) == 7) {
503 }
else if ((bcsp->
rx_skb->data[1] & 0x0f) == 1 &&
504 !(bcsp->
rx_skb->data[0] & 0x80)) {
505 bcsp_handle_le_pkt(hu);
512 u8 desc = (bcsp->
rx_skb->data[1] & 0x0f);
514 if (desc != 0 && desc != 1) {
521 hdr.plen = bcsp->
rx_skb->len;
527 BT_ERR (
"Packet for unknown channel (%u %s)",
528 bcsp->
rx_skb->data[1] & 0x0f,
529 bcsp->
rx_skb->data[0] & 0x80 ?
530 "reliable" :
"unreliable");
542 bcsp->
rx_state = BCSP_W4_PKT_DELIMITER;
548 return get_unaligned_be16(&bcsp->
rx_skb->data[bcsp->
rx_skb->len - 2]);
552 static int bcsp_recv(
struct hci_uart *hu,
void *data,
int count)
557 BT_DBG(
"hu %p count %d rx_state %d rx_count %ld",
564 BT_ERR(
"Short BCSP packet");
569 bcsp_unslip_one_byte(bcsp, *ptr);
577 if ((0xff & (
u8) ~ (bcsp->
rx_skb->data[0] + bcsp->
rx_skb->data[1] +
579 BT_ERR(
"Error in BCSP hdr checksum");
585 if (bcsp->
rx_skb->data[0] & 0x80
587 BT_ERR (
"Out-of-order packet arrived, got %u expected %u",
597 (bcsp->
rx_skb->data[2] << 4);
601 if (bcsp->
rx_skb->data[0] & 0x40) {
605 bcsp_complete_rx_pkt(hu);
610 BT_ERR (
"Checksum failed: computed %04x received %04x",
620 bcsp_complete_rx_pkt(hu);
653 BT_ERR(
"Can't allocate mem for new packet");
668 static void bcsp_timed_event(
unsigned long arg)
675 BT_DBG(
"hu %p retransmitting %u pkts", hu, bcsp->
unack.qlen);
679 while ((skb = __skb_dequeue_tail(&bcsp->
unack)) !=
NULL) {
684 spin_unlock_irqrestore(&bcsp->
unack.lock, flags);
689 static int bcsp_open(
struct hci_uart *hu)
700 skb_queue_head_init(&bcsp->
unack);
701 skb_queue_head_init(&bcsp->
rel);
702 skb_queue_head_init(&bcsp->
unrel);
705 bcsp->
tbcsp.function = bcsp_timed_event;
716 static int bcsp_close(
struct hci_uart *hu)
736 .enqueue = bcsp_enqueue,
737 .dequeue = bcsp_dequeue,
747 BT_INFO(
"HCI BCSP protocol initialized");
749 BT_ERR(
"HCI BCSP protocol registration failed");