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