1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Bluetooth HCI UART driver 5 * 6 * Copyright (C) 2002-2003 Fabrizio Gennari <fabrizio.gennari@philips.com> 7 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> 8 */ 9 10 #include <linux/module.h> 11 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> 19 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 <linux/crc-ccitt.h> 29 #include <linux/unaligned.h> 30 31 #include <net/bluetooth/bluetooth.h> 32 #include <net/bluetooth/hci_core.h> 33 34 #include "hci_uart.h" 35 36 static bool txcrc = true; 37 static bool hciextn = true; 38 39 #define BCSP_TXWINSIZE 4 40 41 #define BCSP_ACK_PKT 0x05 42 #define BCSP_LE_PKT 0x06 43 44 struct bcsp_struct { 45 struct sk_buff_head unack; /* Unack'ed packets queue */ 46 struct sk_buff_head rel; /* Reliable packets queue */ 47 struct sk_buff_head unrel; /* Unreliable packets queue */ 48 49 unsigned long rx_count; 50 struct sk_buff *rx_skb; 51 u8 rxseq_txack; /* rxseq == txack. */ 52 u8 rxack; /* Last packet sent by us that the peer ack'ed */ 53 struct timer_list tbcsp; 54 struct hci_uart *hu; 55 56 enum { 57 BCSP_W4_PKT_DELIMITER, 58 BCSP_W4_PKT_START, 59 BCSP_W4_BCSP_HDR, 60 BCSP_W4_DATA, 61 BCSP_W4_CRC 62 } rx_state; 63 64 enum { 65 BCSP_ESCSTATE_NOESC, 66 BCSP_ESCSTATE_ESC 67 } rx_esc_state; 68 69 u8 use_crc; 70 u16 message_crc; 71 u8 txack_req; /* Do we need to send ack's to the peer? */ 72 73 /* Reliable packet sequence number - used to assign seq to each rel pkt. */ 74 u8 msgq_txseq; 75 }; 76 77 /* ---- BCSP CRC calculation ---- */ 78 79 /* Initialise the crc calculator */ 80 #define BCSP_CRC_INIT(x) x = 0xffff 81 82 /* Update crc with next data byte */ 83 static void bcsp_crc_update(u16 *crc, u8 d) 84 { 85 *crc = crc_ccitt_byte(*crc, d); 86 } 87 88 /* ---- BCSP core ---- */ 89 90 static void bcsp_slip_msgdelim(struct sk_buff *skb) 91 { 92 const char pkt_delim = 0xc0; 93 94 skb_put_data(skb, &pkt_delim, 1); 95 } 96 97 static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c) 98 { 99 const char esc_c0[2] = { 0xdb, 0xdc }; 100 const char esc_db[2] = { 0xdb, 0xdd }; 101 102 switch (c) { 103 case 0xc0: 104 skb_put_data(skb, &esc_c0, 2); 105 break; 106 case 0xdb: 107 skb_put_data(skb, &esc_db, 2); 108 break; 109 default: 110 skb_put_data(skb, &c, 1); 111 } 112 } 113 114 static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb) 115 { 116 struct bcsp_struct *bcsp = hu->priv; 117 118 if (skb->len > 0xFFF) { 119 BT_ERR("Packet too long"); 120 kfree_skb(skb); 121 return 0; 122 } 123 124 switch (hci_skb_pkt_type(skb)) { 125 case HCI_ACLDATA_PKT: 126 case HCI_COMMAND_PKT: 127 skb_queue_tail(&bcsp->rel, skb); 128 break; 129 130 case HCI_SCODATA_PKT: 131 skb_queue_tail(&bcsp->unrel, skb); 132 break; 133 134 default: 135 BT_ERR("Unknown packet type"); 136 kfree_skb(skb); 137 break; 138 } 139 140 return 0; 141 } 142 143 static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data, 144 int len, int pkt_type) 145 { 146 struct sk_buff *nskb; 147 u8 hdr[4], chan; 148 u16 BCSP_CRC_INIT(bcsp_txmsg_crc); 149 int rel, i; 150 151 switch (pkt_type) { 152 case HCI_ACLDATA_PKT: 153 chan = 6; /* BCSP ACL channel */ 154 rel = 1; /* reliable channel */ 155 break; 156 case HCI_COMMAND_PKT: 157 chan = 5; /* BCSP cmd/evt channel */ 158 rel = 1; /* reliable channel */ 159 break; 160 case HCI_SCODATA_PKT: 161 chan = 7; /* BCSP SCO channel */ 162 rel = 0; /* unreliable channel */ 163 break; 164 case BCSP_LE_PKT: 165 chan = 1; /* BCSP LE channel */ 166 rel = 0; /* unreliable channel */ 167 break; 168 case BCSP_ACK_PKT: 169 chan = 0; /* BCSP internal channel */ 170 rel = 0; /* unreliable channel */ 171 break; 172 default: 173 BT_ERR("Unknown packet type"); 174 return NULL; 175 } 176 177 if (hciextn && chan == 5 && len > HCI_COMMAND_HDR_SIZE) { 178 __le16 opcode = ((struct hci_command_hdr *)data)->opcode; 179 180 /* Vendor specific commands */ 181 if (hci_opcode_ogf(__le16_to_cpu(opcode)) == 0x3f) { 182 u8 desc = *(data + HCI_COMMAND_HDR_SIZE); 183 184 if ((desc & 0xf0) == 0xc0) { 185 data += HCI_COMMAND_HDR_SIZE + 1; 186 len -= HCI_COMMAND_HDR_SIZE + 1; 187 chan = desc & 0x0f; 188 } 189 } 190 } 191 192 /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2 193 * (because bytes 0xc0 and 0xdb are escaped, worst case is 194 * when the packet is all made of 0xc0 and 0xdb :) ) 195 * + 2 (0xc0 delimiters at start and end). 196 */ 197 198 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC); 199 if (!nskb) 200 return NULL; 201 202 hci_skb_pkt_type(nskb) = pkt_type; 203 204 bcsp_slip_msgdelim(nskb); 205 206 hdr[0] = bcsp->rxseq_txack << 3; 207 bcsp->txack_req = 0; 208 BT_DBG("We request packet no %u to card", bcsp->rxseq_txack); 209 210 if (rel) { 211 hdr[0] |= 0x80 + bcsp->msgq_txseq; 212 BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq); 213 bcsp->msgq_txseq = (bcsp->msgq_txseq + 1) & 0x07; 214 } 215 216 if (bcsp->use_crc) 217 hdr[0] |= 0x40; 218 219 hdr[1] = ((len << 4) & 0xff) | chan; 220 hdr[2] = len >> 4; 221 hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]); 222 223 /* Put BCSP header */ 224 for (i = 0; i < 4; i++) { 225 bcsp_slip_one_byte(nskb, hdr[i]); 226 227 if (bcsp->use_crc) 228 bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]); 229 } 230 231 /* Put payload */ 232 for (i = 0; i < len; i++) { 233 bcsp_slip_one_byte(nskb, data[i]); 234 235 if (bcsp->use_crc) 236 bcsp_crc_update(&bcsp_txmsg_crc, data[i]); 237 } 238 239 /* Put CRC */ 240 if (bcsp->use_crc) { 241 bcsp_txmsg_crc = bitrev16(bcsp_txmsg_crc); 242 bcsp_slip_one_byte(nskb, (u8)((bcsp_txmsg_crc >> 8) & 0x00ff)); 243 bcsp_slip_one_byte(nskb, (u8)(bcsp_txmsg_crc & 0x00ff)); 244 } 245 246 bcsp_slip_msgdelim(nskb); 247 return nskb; 248 } 249 250 /* This is a rewrite of pkt_avail in ABCSP */ 251 static struct sk_buff *bcsp_dequeue(struct hci_uart *hu) 252 { 253 struct bcsp_struct *bcsp = hu->priv; 254 unsigned long flags; 255 struct sk_buff *skb; 256 257 /* First of all, check for unreliable messages in the queue, 258 * since they have priority 259 */ 260 261 skb = skb_dequeue(&bcsp->unrel); 262 if (skb != NULL) { 263 struct sk_buff *nskb; 264 265 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, 266 hci_skb_pkt_type(skb)); 267 if (nskb) { 268 kfree_skb(skb); 269 return nskb; 270 } else { 271 skb_queue_head(&bcsp->unrel, skb); 272 BT_ERR("Could not dequeue pkt because alloc_skb failed"); 273 } 274 } 275 276 /* Now, try to send a reliable pkt. We can only send a 277 * reliable packet if the number of packets sent but not yet ack'ed 278 * is < than the winsize 279 */ 280 281 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING); 282 283 if (bcsp->unack.qlen < BCSP_TXWINSIZE) { 284 skb = skb_dequeue(&bcsp->rel); 285 if (skb != NULL) { 286 struct sk_buff *nskb; 287 288 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, 289 hci_skb_pkt_type(skb)); 290 if (nskb) { 291 __skb_queue_tail(&bcsp->unack, skb); 292 mod_timer(&bcsp->tbcsp, jiffies + HZ / 4); 293 spin_unlock_irqrestore(&bcsp->unack.lock, flags); 294 return nskb; 295 } else { 296 skb_queue_head(&bcsp->rel, skb); 297 BT_ERR("Could not dequeue pkt because alloc_skb failed"); 298 } 299 } 300 } 301 302 spin_unlock_irqrestore(&bcsp->unack.lock, flags); 303 304 /* We could not send a reliable packet, either because there are 305 * none or because there are too many unack'ed pkts. Did we receive 306 * any packets we have not acknowledged yet ? 307 */ 308 309 if (bcsp->txack_req) { 310 /* if so, craft an empty ACK pkt and send it on BCSP unreliable 311 * channel 0 312 */ 313 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT); 314 return nskb; 315 } 316 317 /* We have nothing to send */ 318 return NULL; 319 } 320 321 static int bcsp_flush(struct hci_uart *hu) 322 { 323 BT_DBG("hu %p", hu); 324 return 0; 325 } 326 327 /* Remove ack'ed packets */ 328 static void bcsp_pkt_cull(struct bcsp_struct *bcsp) 329 { 330 struct sk_buff *skb, *tmp; 331 unsigned long flags; 332 int i, pkts_to_be_removed; 333 u8 seqno; 334 335 spin_lock_irqsave(&bcsp->unack.lock, flags); 336 337 pkts_to_be_removed = skb_queue_len(&bcsp->unack); 338 seqno = bcsp->msgq_txseq; 339 340 while (pkts_to_be_removed) { 341 if (bcsp->rxack == seqno) 342 break; 343 pkts_to_be_removed--; 344 seqno = (seqno - 1) & 0x07; 345 } 346 347 if (bcsp->rxack != seqno) 348 BT_ERR("Peer acked invalid packet"); 349 350 BT_DBG("Removing %u pkts out of %u, up to seqno %u", 351 pkts_to_be_removed, skb_queue_len(&bcsp->unack), 352 (seqno - 1) & 0x07); 353 354 i = 0; 355 skb_queue_walk_safe(&bcsp->unack, skb, tmp) { 356 if (i >= pkts_to_be_removed) 357 break; 358 i++; 359 360 __skb_unlink(skb, &bcsp->unack); 361 dev_kfree_skb_irq(skb); 362 } 363 364 if (skb_queue_empty(&bcsp->unack)) 365 timer_delete(&bcsp->tbcsp); 366 367 spin_unlock_irqrestore(&bcsp->unack.lock, flags); 368 369 if (i != pkts_to_be_removed) 370 BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed); 371 } 372 373 /* Handle BCSP link-establishment packets. When we 374 * detect a "sync" packet, symptom that the BT module has reset, 375 * we do nothing :) (yet) 376 */ 377 static void bcsp_handle_le_pkt(struct hci_uart *hu) 378 { 379 struct bcsp_struct *bcsp = hu->priv; 380 u8 conf_pkt[4] = { 0xad, 0xef, 0xac, 0xed }; 381 u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 }; 382 u8 sync_pkt[4] = { 0xda, 0xdc, 0xed, 0xed }; 383 384 /* spot "conf" pkts and reply with a "conf rsp" pkt */ 385 if (bcsp->rx_skb->len < 8) 386 return; 387 388 if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 && 389 !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) { 390 struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC); 391 392 BT_DBG("Found a LE conf pkt"); 393 if (!nskb) 394 return; 395 skb_put_data(nskb, conf_rsp_pkt, 4); 396 hci_skb_pkt_type(nskb) = BCSP_LE_PKT; 397 398 skb_queue_head(&bcsp->unrel, nskb); 399 hci_uart_tx_wakeup(hu); 400 } 401 /* Spot "sync" pkts. If we find one...disaster! */ 402 else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 && 403 !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) { 404 BT_ERR("Found a LE sync pkt, card has reset"); 405 } 406 } 407 408 static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte) 409 { 410 const u8 c0 = 0xc0, db = 0xdb; 411 412 switch (bcsp->rx_esc_state) { 413 case BCSP_ESCSTATE_NOESC: 414 switch (byte) { 415 case 0xdb: 416 bcsp->rx_esc_state = BCSP_ESCSTATE_ESC; 417 break; 418 default: 419 skb_put_data(bcsp->rx_skb, &byte, 1); 420 if ((bcsp->rx_skb->data[0] & 0x40) != 0 && 421 bcsp->rx_state != BCSP_W4_CRC) 422 bcsp_crc_update(&bcsp->message_crc, byte); 423 bcsp->rx_count--; 424 } 425 break; 426 427 case BCSP_ESCSTATE_ESC: 428 switch (byte) { 429 case 0xdc: 430 skb_put_data(bcsp->rx_skb, &c0, 1); 431 if ((bcsp->rx_skb->data[0] & 0x40) != 0 && 432 bcsp->rx_state != BCSP_W4_CRC) 433 bcsp_crc_update(&bcsp->message_crc, 0xc0); 434 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC; 435 bcsp->rx_count--; 436 break; 437 438 case 0xdd: 439 skb_put_data(bcsp->rx_skb, &db, 1); 440 if ((bcsp->rx_skb->data[0] & 0x40) != 0 && 441 bcsp->rx_state != BCSP_W4_CRC) 442 bcsp_crc_update(&bcsp->message_crc, 0xdb); 443 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC; 444 bcsp->rx_count--; 445 break; 446 447 default: 448 BT_ERR("Invalid byte %02x after esc byte", byte); 449 kfree_skb(bcsp->rx_skb); 450 bcsp->rx_skb = NULL; 451 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 452 bcsp->rx_count = 0; 453 } 454 } 455 } 456 457 static void bcsp_complete_rx_pkt(struct hci_uart *hu) 458 { 459 struct bcsp_struct *bcsp = hu->priv; 460 int pass_up = 0; 461 462 if (bcsp->rx_skb->data[0] & 0x80) { /* reliable pkt */ 463 BT_DBG("Received seqno %u from card", bcsp->rxseq_txack); 464 465 /* check the rx sequence number is as expected */ 466 if ((bcsp->rx_skb->data[0] & 0x07) == bcsp->rxseq_txack) { 467 bcsp->rxseq_txack++; 468 bcsp->rxseq_txack %= 0x8; 469 } else { 470 /* handle re-transmitted packet or 471 * when packet was missed 472 */ 473 BT_ERR("Out-of-order packet arrived, got %u expected %u", 474 bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack); 475 476 /* do not process out-of-order packet payload */ 477 pass_up = 2; 478 } 479 480 /* send current txack value to all received reliable packets */ 481 bcsp->txack_req = 1; 482 483 /* If needed, transmit an ack pkt */ 484 hci_uart_tx_wakeup(hu); 485 } 486 487 bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07; 488 BT_DBG("Request for pkt %u from card", bcsp->rxack); 489 490 /* handle received ACK indications, 491 * including those from out-of-order packets 492 */ 493 bcsp_pkt_cull(bcsp); 494 495 if (pass_up != 2) { 496 if ((bcsp->rx_skb->data[1] & 0x0f) == 6 && 497 (bcsp->rx_skb->data[0] & 0x80)) { 498 hci_skb_pkt_type(bcsp->rx_skb) = HCI_ACLDATA_PKT; 499 pass_up = 1; 500 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 && 501 (bcsp->rx_skb->data[0] & 0x80)) { 502 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT; 503 pass_up = 1; 504 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) { 505 hci_skb_pkt_type(bcsp->rx_skb) = HCI_SCODATA_PKT; 506 pass_up = 1; 507 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 && 508 !(bcsp->rx_skb->data[0] & 0x80)) { 509 bcsp_handle_le_pkt(hu); 510 pass_up = 0; 511 } else { 512 pass_up = 0; 513 } 514 } 515 516 if (pass_up == 0) { 517 struct hci_event_hdr hdr; 518 u8 desc = (bcsp->rx_skb->data[1] & 0x0f); 519 520 if (desc != 0 && desc != 1) { 521 if (hciextn) { 522 desc |= 0xc0; 523 skb_pull(bcsp->rx_skb, 4); 524 memcpy(skb_push(bcsp->rx_skb, 1), &desc, 1); 525 526 hdr.evt = 0xff; 527 hdr.plen = bcsp->rx_skb->len; 528 memcpy(skb_push(bcsp->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE); 529 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT; 530 531 hci_recv_frame(hu->hdev, bcsp->rx_skb); 532 } else { 533 BT_ERR("Packet for unknown channel (%u %s)", 534 bcsp->rx_skb->data[1] & 0x0f, 535 bcsp->rx_skb->data[0] & 0x80 ? 536 "reliable" : "unreliable"); 537 kfree_skb(bcsp->rx_skb); 538 } 539 } else 540 kfree_skb(bcsp->rx_skb); 541 } else if (pass_up == 1) { 542 /* Pull out BCSP hdr */ 543 skb_pull(bcsp->rx_skb, 4); 544 545 hci_recv_frame(hu->hdev, bcsp->rx_skb); 546 } else { 547 /* ignore packet payload of already ACKed re-transmitted 548 * packets or when a packet was missed in the BCSP window 549 */ 550 kfree_skb(bcsp->rx_skb); 551 } 552 553 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 554 bcsp->rx_skb = NULL; 555 } 556 557 static u16 bscp_get_crc(struct bcsp_struct *bcsp) 558 { 559 return get_unaligned_be16(&bcsp->rx_skb->data[bcsp->rx_skb->len - 2]); 560 } 561 562 /* Recv data */ 563 static int bcsp_recv(struct hci_uart *hu, const void *data, int count) 564 { 565 struct bcsp_struct *bcsp = hu->priv; 566 const unsigned char *ptr; 567 568 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 569 return -EUNATCH; 570 571 if (!bcsp) 572 return -ENODEV; 573 574 BT_DBG("hu %p count %d rx_state %d rx_count %ld", 575 hu, count, bcsp->rx_state, bcsp->rx_count); 576 577 ptr = data; 578 while (count) { 579 if (bcsp->rx_count) { 580 if (*ptr == 0xc0) { 581 BT_ERR("Short BCSP packet"); 582 kfree_skb(bcsp->rx_skb); 583 bcsp->rx_skb = NULL; 584 bcsp->rx_state = BCSP_W4_PKT_START; 585 bcsp->rx_count = 0; 586 } else 587 bcsp_unslip_one_byte(bcsp, *ptr); 588 589 ptr++; count--; 590 continue; 591 } 592 593 switch (bcsp->rx_state) { 594 case BCSP_W4_BCSP_HDR: 595 if ((0xff & (u8)~(bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] + 596 bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) { 597 BT_ERR("Error in BCSP hdr checksum"); 598 kfree_skb(bcsp->rx_skb); 599 bcsp->rx_skb = NULL; 600 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 601 bcsp->rx_count = 0; 602 continue; 603 } 604 bcsp->rx_state = BCSP_W4_DATA; 605 bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) + 606 (bcsp->rx_skb->data[2] << 4); /* May be 0 */ 607 continue; 608 609 case BCSP_W4_DATA: 610 if (bcsp->rx_skb->data[0] & 0x40) { /* pkt with crc */ 611 bcsp->rx_state = BCSP_W4_CRC; 612 bcsp->rx_count = 2; 613 } else 614 bcsp_complete_rx_pkt(hu); 615 continue; 616 617 case BCSP_W4_CRC: 618 if (bitrev16(bcsp->message_crc) != bscp_get_crc(bcsp)) { 619 BT_ERR("Checksum failed: computed %04x received %04x", 620 bitrev16(bcsp->message_crc), 621 bscp_get_crc(bcsp)); 622 623 kfree_skb(bcsp->rx_skb); 624 bcsp->rx_skb = NULL; 625 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 626 bcsp->rx_count = 0; 627 continue; 628 } 629 skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2); 630 bcsp_complete_rx_pkt(hu); 631 continue; 632 633 case BCSP_W4_PKT_DELIMITER: 634 switch (*ptr) { 635 case 0xc0: 636 bcsp->rx_state = BCSP_W4_PKT_START; 637 break; 638 default: 639 /*BT_ERR("Ignoring byte %02x", *ptr);*/ 640 break; 641 } 642 ptr++; count--; 643 break; 644 645 case BCSP_W4_PKT_START: 646 switch (*ptr) { 647 case 0xc0: 648 ptr++; count--; 649 break; 650 651 default: 652 bcsp->rx_state = BCSP_W4_BCSP_HDR; 653 bcsp->rx_count = 4; 654 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC; 655 BCSP_CRC_INIT(bcsp->message_crc); 656 657 /* Do not increment ptr or decrement count 658 * Allocate packet. Max len of a BCSP pkt= 659 * 0xFFF (payload) +4 (header) +2 (crc) 660 */ 661 662 bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC); 663 if (!bcsp->rx_skb) { 664 BT_ERR("Can't allocate mem for new packet"); 665 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 666 bcsp->rx_count = 0; 667 return 0; 668 } 669 break; 670 } 671 break; 672 } 673 } 674 return count; 675 } 676 677 /* Arrange to retransmit all messages in the relq. */ 678 static void bcsp_timed_event(struct timer_list *t) 679 { 680 struct bcsp_struct *bcsp = timer_container_of(bcsp, t, tbcsp); 681 struct hci_uart *hu = bcsp->hu; 682 struct sk_buff *skb; 683 unsigned long flags; 684 685 BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen); 686 687 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING); 688 689 while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) { 690 bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07; 691 skb_queue_head(&bcsp->rel, skb); 692 } 693 694 spin_unlock_irqrestore(&bcsp->unack.lock, flags); 695 696 hci_uart_tx_wakeup(hu); 697 } 698 699 static int bcsp_open(struct hci_uart *hu) 700 { 701 struct bcsp_struct *bcsp; 702 703 BT_DBG("hu %p", hu); 704 705 bcsp = kzalloc_obj(*bcsp); 706 if (!bcsp) 707 return -ENOMEM; 708 709 hu->priv = bcsp; 710 bcsp->hu = hu; 711 skb_queue_head_init(&bcsp->unack); 712 skb_queue_head_init(&bcsp->rel); 713 skb_queue_head_init(&bcsp->unrel); 714 715 timer_setup(&bcsp->tbcsp, bcsp_timed_event, 0); 716 717 bcsp->rx_state = BCSP_W4_PKT_DELIMITER; 718 719 if (txcrc) 720 bcsp->use_crc = 1; 721 722 return 0; 723 } 724 725 static int bcsp_close(struct hci_uart *hu) 726 { 727 struct bcsp_struct *bcsp = hu->priv; 728 729 timer_shutdown_sync(&bcsp->tbcsp); 730 731 hu->priv = NULL; 732 733 BT_DBG("hu %p", hu); 734 735 skb_queue_purge(&bcsp->unack); 736 skb_queue_purge(&bcsp->rel); 737 skb_queue_purge(&bcsp->unrel); 738 739 if (bcsp->rx_skb) { 740 kfree_skb(bcsp->rx_skb); 741 bcsp->rx_skb = NULL; 742 } 743 744 kfree(bcsp); 745 return 0; 746 } 747 748 static const struct hci_uart_proto bcsp = { 749 .id = HCI_UART_BCSP, 750 .name = "BCSP", 751 .open = bcsp_open, 752 .close = bcsp_close, 753 .enqueue = bcsp_enqueue, 754 .dequeue = bcsp_dequeue, 755 .recv = bcsp_recv, 756 .flush = bcsp_flush 757 }; 758 759 int __init bcsp_init(void) 760 { 761 return hci_uart_register_proto(&bcsp); 762 } 763 764 int __exit bcsp_deinit(void) 765 { 766 return hci_uart_unregister_proto(&bcsp); 767 } 768 769 module_param(txcrc, bool, 0644); 770 MODULE_PARM_DESC(txcrc, "Transmit CRC with every BCSP packet"); 771 772 module_param(hciextn, bool, 0644); 773 MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into BCSP packets"); 774