blob: 27829273f3c913fe53adef14ebd3ccb89b9d0fd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Marcel Holtmann0372a662005-10-28 19:20:45 +02002 *
3 * Bluetooth HCI UART driver
4 *
5 * Copyright (C) 2002-2003 Fabrizio Gennari <fabrizio.gennari@philips.com>
6 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
27#include <linux/kernel.h>
28#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/types.h>
30#include <linux/fcntl.h>
31#include <linux/interrupt.h>
32#include <linux/ptrace.h>
33#include <linux/poll.h>
34
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>
40#include <linux/ioctl.h>
41#include <linux/skbuff.h>
Harvey Harrisonc5ec5142008-06-10 12:48:45 -070042#include <linux/bitrev.h>
43#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
Marcel Holtmann0372a662005-10-28 19:20:45 +020047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "hci_uart.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Shailendra Vermabff6b892015-05-26 00:00:57 +053050static bool txcrc = true;
51static bool hciextn = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Marcel Holtmann0372a662005-10-28 19:20:45 +020053#define BCSP_TXWINSIZE 4
54
55#define BCSP_ACK_PKT 0x05
56#define BCSP_LE_PKT 0x06
57
58struct bcsp_struct {
59 struct sk_buff_head unack; /* Unack'ed packets queue */
60 struct sk_buff_head rel; /* Reliable packets queue */
61 struct sk_buff_head unrel; /* Unreliable packets queue */
62
63 unsigned long rx_count;
64 struct sk_buff *rx_skb;
65 u8 rxseq_txack; /* rxseq == txack. */
66 u8 rxack; /* Last packet sent by us that the peer ack'ed */
67 struct timer_list tbcsp;
Kees Cook04356052017-10-04 17:54:29 -070068 struct hci_uart *hu;
Marcel Holtmann0372a662005-10-28 19:20:45 +020069
70 enum {
71 BCSP_W4_PKT_DELIMITER,
72 BCSP_W4_PKT_START,
73 BCSP_W4_BCSP_HDR,
74 BCSP_W4_DATA,
75 BCSP_W4_CRC
76 } rx_state;
77
78 enum {
79 BCSP_ESCSTATE_NOESC,
80 BCSP_ESCSTATE_ESC
81 } rx_esc_state;
82
83 u8 use_crc;
84 u16 message_crc;
85 u8 txack_req; /* Do we need to send ack's to the peer? */
86
87 /* Reliable packet sequence number - used to assign seq to each rel pkt. */
88 u8 msgq_txseq;
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* ---- BCSP CRC calculation ---- */
92
93/* Table for calculating CRC for polynomial 0x1021, LSB processed first,
Dean Jenkins8083ad1c2016-09-23 18:56:26 +010094 * initial value 0xffff, bits shifted in reverse order.
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static const u16 crc_table[] = {
98 0x0000, 0x1081, 0x2102, 0x3183,
99 0x4204, 0x5285, 0x6306, 0x7387,
100 0x8408, 0x9489, 0xa50a, 0xb58b,
101 0xc60c, 0xd68d, 0xe70e, 0xf78f
102};
103
104/* Initialise the crc calculator */
105#define BCSP_CRC_INIT(x) x = 0xffff
106
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300107/* Update crc with next data byte
108 *
109 * Implementation note
110 * The data byte is treated as two nibbles. The crc is generated
111 * in reverse, i.e., bits are fed into the register from the top.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void bcsp_crc_update(u16 *crc, u8 d)
114{
115 u16 reg = *crc;
116
117 reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
118 reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
119
120 *crc = reg;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* ---- BCSP core ---- */
124
125static void bcsp_slip_msgdelim(struct sk_buff *skb)
126{
127 const char pkt_delim = 0xc0;
Marcel Holtmann0372a662005-10-28 19:20:45 +0200128
Johannes Berg59ae1d12017-06-16 14:29:20 +0200129 skb_put_data(skb, &pkt_delim, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
133{
134 const char esc_c0[2] = { 0xdb, 0xdc };
135 const char esc_db[2] = { 0xdb, 0xdd };
136
137 switch (c) {
138 case 0xc0:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200139 skb_put_data(skb, &esc_c0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141 case 0xdb:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200142 skb_put_data(skb, &esc_db, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 default:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200145 skb_put_data(skb, &c, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147}
148
149static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
150{
151 struct bcsp_struct *bcsp = hu->priv;
152
153 if (skb->len > 0xFFF) {
154 BT_ERR("Packet too long");
155 kfree_skb(skb);
156 return 0;
157 }
158
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100159 switch (hci_skb_pkt_type(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 case HCI_ACLDATA_PKT:
161 case HCI_COMMAND_PKT:
162 skb_queue_tail(&bcsp->rel, skb);
163 break;
164
165 case HCI_SCODATA_PKT:
166 skb_queue_tail(&bcsp->unrel, skb);
167 break;
168
169 default:
170 BT_ERR("Unknown packet type");
171 kfree_skb(skb);
172 break;
173 }
174
175 return 0;
176}
177
178static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100179 int len, int pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct sk_buff *nskb;
182 u8 hdr[4], chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200184 int rel, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 switch (pkt_type) {
187 case HCI_ACLDATA_PKT:
188 chan = 6; /* BCSP ACL channel */
189 rel = 1; /* reliable channel */
190 break;
191 case HCI_COMMAND_PKT:
192 chan = 5; /* BCSP cmd/evt channel */
193 rel = 1; /* reliable channel */
194 break;
195 case HCI_SCODATA_PKT:
196 chan = 7; /* BCSP SCO channel */
197 rel = 0; /* unreliable channel */
198 break;
199 case BCSP_LE_PKT:
200 chan = 1; /* BCSP LE channel */
201 rel = 0; /* unreliable channel */
202 break;
203 case BCSP_ACK_PKT:
204 chan = 0; /* BCSP internal channel */
205 rel = 0; /* unreliable channel */
206 break;
207 default:
208 BT_ERR("Unknown packet type");
209 return NULL;
210 }
211
212 if (hciextn && chan == 5) {
Harvey Harrisonc5ec5142008-06-10 12:48:45 -0700213 __le16 opcode = ((struct hci_command_hdr *)data)->opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200215 /* Vendor specific commands */
Harvey Harrisonc5ec5142008-06-10 12:48:45 -0700216 if (hci_opcode_ogf(__le16_to_cpu(opcode)) == 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u8 desc = *(data + HCI_COMMAND_HDR_SIZE);
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if ((desc & 0xf0) == 0xc0) {
220 data += HCI_COMMAND_HDR_SIZE + 1;
221 len -= HCI_COMMAND_HDR_SIZE + 1;
222 chan = desc & 0x0f;
223 }
224 }
225 }
226
227 /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300228 * (because bytes 0xc0 and 0xdb are escaped, worst case is
229 * when the packet is all made of 0xc0 and 0xdb :) )
230 * + 2 (0xc0 delimiters at start and end).
231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
234 if (!nskb)
235 return NULL;
236
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100237 hci_skb_pkt_type(nskb) = pkt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 bcsp_slip_msgdelim(nskb);
240
241 hdr[0] = bcsp->rxseq_txack << 3;
242 bcsp->txack_req = 0;
243 BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
244
245 if (rel) {
246 hdr[0] |= 0x80 + bcsp->msgq_txseq;
247 BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
David Howellsdd1589a2010-06-30 13:10:09 -0700248 bcsp->msgq_txseq = (bcsp->msgq_txseq + 1) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200250
251 if (bcsp->use_crc)
252 hdr[0] |= 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 hdr[1] = ((len << 4) & 0xff) | chan;
255 hdr[2] = len >> 4;
256 hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
257
258 /* Put BCSP header */
259 for (i = 0; i < 4; i++) {
260 bcsp_slip_one_byte(nskb, hdr[i]);
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200261
262 if (bcsp->use_crc)
263 bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 /* Put payload */
267 for (i = 0; i < len; i++) {
268 bcsp_slip_one_byte(nskb, data[i]);
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200269
270 if (bcsp->use_crc)
271 bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Put CRC */
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200275 if (bcsp->use_crc) {
Harvey Harrisonc5ec5142008-06-10 12:48:45 -0700276 bcsp_txmsg_crc = bitrev16(bcsp_txmsg_crc);
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100277 bcsp_slip_one_byte(nskb, (u8)((bcsp_txmsg_crc >> 8) & 0x00ff));
278 bcsp_slip_one_byte(nskb, (u8)(bcsp_txmsg_crc & 0x00ff));
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 bcsp_slip_msgdelim(nskb);
282 return nskb;
283}
284
285/* This is a rewrite of pkt_avail in ABCSP */
286static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
287{
288 struct bcsp_struct *bcsp = hu->priv;
289 unsigned long flags;
290 struct sk_buff *skb;
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* First of all, check for unreliable messages in the queue,
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100293 * since they have priority
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Valentin Iliea08b15e2013-08-12 18:46:00 +0300296 skb = skb_dequeue(&bcsp->unrel);
297 if (skb != NULL) {
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100298 struct sk_buff *nskb;
299
300 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
301 hci_skb_pkt_type(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (nskb) {
303 kfree_skb(skb);
304 return nskb;
305 } else {
306 skb_queue_head(&bcsp->unrel, skb);
307 BT_ERR("Could not dequeue pkt because alloc_skb failed");
308 }
309 }
310
311 /* Now, try to send a reliable pkt. We can only send a
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300312 * reliable packet if the number of packets sent but not yet ack'ed
313 * is < than the winsize
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Peter Zijlstraf89d75f2006-12-06 20:36:59 -0800316 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Valentin Iliea08b15e2013-08-12 18:46:00 +0300318 if (bcsp->unack.qlen < BCSP_TXWINSIZE) {
319 skb = skb_dequeue(&bcsp->rel);
320 if (skb != NULL) {
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100321 struct sk_buff *nskb;
322
323 nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
324 hci_skb_pkt_type(skb));
Valentin Iliea08b15e2013-08-12 18:46:00 +0300325 if (nskb) {
326 __skb_queue_tail(&bcsp->unack, skb);
327 mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
328 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
329 return nskb;
330 } else {
331 skb_queue_head(&bcsp->rel, skb);
332 BT_ERR("Could not dequeue pkt because alloc_skb failed");
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
336
337 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* We could not send a reliable packet, either because there are
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300340 * none or because there are too many unack'ed pkts. Did we receive
341 * any packets we have not acknowledged yet ?
342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (bcsp->txack_req) {
345 /* if so, craft an empty ACK pkt and send it on BCSP unreliable
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300346 * channel 0
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
349 return nskb;
350 }
351
352 /* We have nothing to send */
353 return NULL;
354}
355
356static int bcsp_flush(struct hci_uart *hu)
357{
358 BT_DBG("hu %p", hu);
359 return 0;
360}
361
362/* Remove ack'ed packets */
363static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
364{
David S. Miller8fc53872008-09-21 22:44:08 -0700365 struct sk_buff *skb, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int i, pkts_to_be_removed;
368 u8 seqno;
369
370 spin_lock_irqsave(&bcsp->unack.lock, flags);
371
David S. Miller8fc53872008-09-21 22:44:08 -0700372 pkts_to_be_removed = skb_queue_len(&bcsp->unack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 seqno = bcsp->msgq_txseq;
374
375 while (pkts_to_be_removed) {
376 if (bcsp->rxack == seqno)
377 break;
378 pkts_to_be_removed--;
379 seqno = (seqno - 1) & 0x07;
380 }
381
382 if (bcsp->rxack != seqno)
383 BT_ERR("Peer acked invalid packet");
384
385 BT_DBG("Removing %u pkts out of %u, up to seqno %u",
David S. Miller8fc53872008-09-21 22:44:08 -0700386 pkts_to_be_removed, skb_queue_len(&bcsp->unack),
387 (seqno - 1) & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David S. Miller8fc53872008-09-21 22:44:08 -0700389 i = 0;
390 skb_queue_walk_safe(&bcsp->unack, skb, tmp) {
Wending Wengd2e353f2009-08-24 16:05:17 -0400391 if (i >= pkts_to_be_removed)
David S. Miller8fc53872008-09-21 22:44:08 -0700392 break;
Wending Wengd2e353f2009-08-24 16:05:17 -0400393 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 __skb_unlink(skb, &bcsp->unack);
396 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Marcel Holtmann0372a662005-10-28 19:20:45 +0200398
David S. Miller8fc53872008-09-21 22:44:08 -0700399 if (skb_queue_empty(&bcsp->unack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 del_timer(&bcsp->tbcsp);
Marcel Holtmann0372a662005-10-28 19:20:45 +0200401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
403
404 if (i != pkts_to_be_removed)
405 BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
406}
407
408/* Handle BCSP link-establishment packets. When we
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300409 * detect a "sync" packet, symptom that the BT module has reset,
410 * we do nothing :) (yet)
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static void bcsp_handle_le_pkt(struct hci_uart *hu)
413{
414 struct bcsp_struct *bcsp = hu->priv;
415 u8 conf_pkt[4] = { 0xad, 0xef, 0xac, 0xed };
416 u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
417 u8 sync_pkt[4] = { 0xda, 0xdc, 0xed, 0xed };
418
419 /* spot "conf" pkts and reply with a "conf rsp" pkt */
420 if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100421 !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
423
424 BT_DBG("Found a LE conf pkt");
425 if (!nskb)
426 return;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200427 skb_put_data(nskb, conf_rsp_pkt, 4);
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100428 hci_skb_pkt_type(nskb) = BCSP_LE_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 skb_queue_head(&bcsp->unrel, nskb);
431 hci_uart_tx_wakeup(hu);
432 }
433 /* Spot "sync" pkts. If we find one...disaster! */
434 else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100435 !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 BT_ERR("Found a LE sync pkt, card has reset");
437 }
438}
439
440static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
441{
442 const u8 c0 = 0xc0, db = 0xdb;
443
444 switch (bcsp->rx_esc_state) {
445 case BCSP_ESCSTATE_NOESC:
446 switch (byte) {
447 case 0xdb:
448 bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
449 break;
450 default:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200451 skb_put_data(bcsp->rx_skb, &byte, 1);
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000452 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100453 bcsp->rx_state != BCSP_W4_CRC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 bcsp_crc_update(&bcsp->message_crc, byte);
455 bcsp->rx_count--;
456 }
457 break;
458
459 case BCSP_ESCSTATE_ESC:
460 switch (byte) {
461 case 0xdc:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200462 skb_put_data(bcsp->rx_skb, &c0, 1);
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000463 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100464 bcsp->rx_state != BCSP_W4_CRC)
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000465 bcsp_crc_update(&bcsp->message_crc, 0xc0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
467 bcsp->rx_count--;
468 break;
469
470 case 0xdd:
Johannes Berg59ae1d12017-06-16 14:29:20 +0200471 skb_put_data(bcsp->rx_skb, &db, 1);
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000472 if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100473 bcsp->rx_state != BCSP_W4_CRC)
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000474 bcsp_crc_update(&bcsp->message_crc, 0xdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
476 bcsp->rx_count--;
477 break;
478
479 default:
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000480 BT_ERR("Invalid byte %02x after esc byte", byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kfree_skb(bcsp->rx_skb);
482 bcsp->rx_skb = NULL;
483 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
484 bcsp->rx_count = 0;
485 }
486 }
487}
488
Arjan van de Ven858119e2006-01-14 13:20:43 -0800489static void bcsp_complete_rx_pkt(struct hci_uart *hu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct bcsp_struct *bcsp = hu->priv;
Dean Jenkins37332dd2016-09-23 18:56:27 +0100492 int pass_up = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (bcsp->rx_skb->data[0] & 0x80) { /* reliable pkt */
495 BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
Dean Jenkins37332dd2016-09-23 18:56:27 +0100496
497 /* check the rx sequence number is as expected */
498 if ((bcsp->rx_skb->data[0] & 0x07) == bcsp->rxseq_txack) {
499 bcsp->rxseq_txack++;
500 bcsp->rxseq_txack %= 0x8;
501 } else {
502 /* handle re-transmitted packet or
503 * when packet was missed
504 */
505 BT_ERR("Out-of-order packet arrived, got %u expected %u",
506 bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
507
508 /* do not process out-of-order packet payload */
509 pass_up = 2;
510 }
511
512 /* send current txack value to all received reliable packets */
513 bcsp->txack_req = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* If needed, transmit an ack pkt */
516 hci_uart_tx_wakeup(hu);
517 }
518
519 bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
520 BT_DBG("Request for pkt %u from card", bcsp->rxack);
521
Dean Jenkins37332dd2016-09-23 18:56:27 +0100522 /* handle received ACK indications,
523 * including those from out-of-order packets
524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 bcsp_pkt_cull(bcsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dean Jenkins37332dd2016-09-23 18:56:27 +0100527 if (pass_up != 2) {
528 if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
529 (bcsp->rx_skb->data[0] & 0x80)) {
530 hci_skb_pkt_type(bcsp->rx_skb) = HCI_ACLDATA_PKT;
531 pass_up = 1;
532 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
533 (bcsp->rx_skb->data[0] & 0x80)) {
534 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
535 pass_up = 1;
536 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
537 hci_skb_pkt_type(bcsp->rx_skb) = HCI_SCODATA_PKT;
538 pass_up = 1;
539 } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
540 !(bcsp->rx_skb->data[0] & 0x80)) {
541 bcsp_handle_le_pkt(hu);
542 pass_up = 0;
543 } else {
544 pass_up = 0;
545 }
546 }
547
548 if (pass_up == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct hci_event_hdr hdr;
550 u8 desc = (bcsp->rx_skb->data[1] & 0x0f);
551
552 if (desc != 0 && desc != 1) {
553 if (hciextn) {
554 desc |= 0xc0;
555 skb_pull(bcsp->rx_skb, 4);
556 memcpy(skb_push(bcsp->rx_skb, 1), &desc, 1);
557
558 hdr.evt = 0xff;
559 hdr.plen = bcsp->rx_skb->len;
560 memcpy(skb_push(bcsp->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE);
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100561 hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700563 hci_recv_frame(hu->hdev, bcsp->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else {
Prasanna Karthikfe8de002015-06-09 11:51:53 +0000565 BT_ERR("Packet for unknown channel (%u %s)",
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100566 bcsp->rx_skb->data[1] & 0x0f,
567 bcsp->rx_skb->data[0] & 0x80 ?
568 "reliable" : "unreliable");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 kfree_skb(bcsp->rx_skb);
570 }
571 } else
572 kfree_skb(bcsp->rx_skb);
Dean Jenkins37332dd2016-09-23 18:56:27 +0100573 } else if (pass_up == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Pull out BCSP hdr */
575 skb_pull(bcsp->rx_skb, 4);
576
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700577 hci_recv_frame(hu->hdev, bcsp->rx_skb);
Dean Jenkins37332dd2016-09-23 18:56:27 +0100578 } else {
579 /* ignore packet payload of already ACKed re-transmitted
580 * packets or when a packet was missed in the BCSP window
581 */
582 kfree_skb(bcsp->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Marcel Holtmann0372a662005-10-28 19:20:45 +0200584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
586 bcsp->rx_skb = NULL;
587}
588
Harvey Harrisonc5ec5142008-06-10 12:48:45 -0700589static u16 bscp_get_crc(struct bcsp_struct *bcsp)
590{
591 return get_unaligned_be16(&bcsp->rx_skb->data[bcsp->rx_skb->len - 2]);
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* Recv data */
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700595static int bcsp_recv(struct hci_uart *hu, const void *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct bcsp_struct *bcsp = hu->priv;
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700598 const unsigned char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300600 BT_DBG("hu %p count %d rx_state %d rx_count %ld",
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100601 hu, count, bcsp->rx_state, bcsp->rx_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 ptr = data;
604 while (count) {
605 if (bcsp->rx_count) {
606 if (*ptr == 0xc0) {
607 BT_ERR("Short BCSP packet");
608 kfree_skb(bcsp->rx_skb);
Tomas Bortoli03bf4872019-11-01 21:42:44 +0100609 bcsp->rx_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 bcsp->rx_state = BCSP_W4_PKT_START;
611 bcsp->rx_count = 0;
612 } else
613 bcsp_unslip_one_byte(bcsp, *ptr);
614
615 ptr++; count--;
616 continue;
617 }
618
619 switch (bcsp->rx_state) {
620 case BCSP_W4_BCSP_HDR:
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100621 if ((0xff & (u8)~(bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
622 bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 BT_ERR("Error in BCSP hdr checksum");
624 kfree_skb(bcsp->rx_skb);
Tomas Bortoli03bf4872019-11-01 21:42:44 +0100625 bcsp->rx_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
627 bcsp->rx_count = 0;
628 continue;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 bcsp->rx_state = BCSP_W4_DATA;
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300631 bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 (bcsp->rx_skb->data[2] << 4); /* May be 0 */
633 continue;
634
635 case BCSP_W4_DATA:
636 if (bcsp->rx_skb->data[0] & 0x40) { /* pkt with crc */
637 bcsp->rx_state = BCSP_W4_CRC;
638 bcsp->rx_count = 2;
639 } else
640 bcsp_complete_rx_pkt(hu);
641 continue;
642
643 case BCSP_W4_CRC:
Harvey Harrisonc5ec5142008-06-10 12:48:45 -0700644 if (bitrev16(bcsp->message_crc) != bscp_get_crc(bcsp)) {
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300645 BT_ERR("Checksum failed: computed %04x received %04x",
Dean Jenkins8083ad1c2016-09-23 18:56:26 +0100646 bitrev16(bcsp->message_crc),
647 bscp_get_crc(bcsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 kfree_skb(bcsp->rx_skb);
Tomas Bortoli03bf4872019-11-01 21:42:44 +0100650 bcsp->rx_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
652 bcsp->rx_count = 0;
653 continue;
654 }
655 skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
656 bcsp_complete_rx_pkt(hu);
657 continue;
658
659 case BCSP_W4_PKT_DELIMITER:
660 switch (*ptr) {
661 case 0xc0:
662 bcsp->rx_state = BCSP_W4_PKT_START;
663 break;
664 default:
665 /*BT_ERR("Ignoring byte %02x", *ptr);*/
666 break;
667 }
668 ptr++; count--;
669 break;
670
671 case BCSP_W4_PKT_START:
672 switch (*ptr) {
673 case 0xc0:
674 ptr++; count--;
675 break;
676
677 default:
678 bcsp->rx_state = BCSP_W4_BCSP_HDR;
679 bcsp->rx_count = 4;
680 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
681 BCSP_CRC_INIT(bcsp->message_crc);
Marcel Holtmann0372a662005-10-28 19:20:45 +0200682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Do not increment ptr or decrement count
Maxim Zhukov8805eea2016-04-08 23:54:51 +0300684 * Allocate packet. Max len of a BCSP pkt=
685 * 0xFFF (payload) +4 (header) +2 (crc)
686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
689 if (!bcsp->rx_skb) {
690 BT_ERR("Can't allocate mem for new packet");
691 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
692 bcsp->rx_count = 0;
693 return 0;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 }
697 break;
698 }
699 }
700 return count;
701}
702
703 /* Arrange to retransmit all messages in the relq. */
Kees Cook04356052017-10-04 17:54:29 -0700704static void bcsp_timed_event(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Kees Cook04356052017-10-04 17:54:29 -0700706 struct bcsp_struct *bcsp = from_timer(bcsp, t, tbcsp);
707 struct hci_uart *hu = bcsp->hu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct sk_buff *skb;
709 unsigned long flags;
710
711 BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen);
712
Peter Zijlstraf89d75f2006-12-06 20:36:59 -0800713 spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
716 bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
717 skb_queue_head(&bcsp->rel, skb);
718 }
719
720 spin_unlock_irqrestore(&bcsp->unack.lock, flags);
721
722 hci_uart_tx_wakeup(hu);
723}
724
725static int bcsp_open(struct hci_uart *hu)
726{
727 struct bcsp_struct *bcsp;
728
729 BT_DBG("hu %p", hu);
730
David Herrmannc063af32012-01-07 15:19:39 +0100731 bcsp = kzalloc(sizeof(*bcsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!bcsp)
733 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 hu->priv = bcsp;
Kees Cook04356052017-10-04 17:54:29 -0700736 bcsp->hu = hu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 skb_queue_head_init(&bcsp->unack);
738 skb_queue_head_init(&bcsp->rel);
739 skb_queue_head_init(&bcsp->unrel);
740
Kees Cook04356052017-10-04 17:54:29 -0700741 timer_setup(&bcsp->tbcsp, bcsp_timed_event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
744
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200745 if (txcrc)
746 bcsp->use_crc = 1;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
751static int bcsp_close(struct hci_uart *hu)
752{
753 struct bcsp_struct *bcsp = hu->priv;
Michael Knudsenc327cdd2014-02-18 09:48:08 +0100754
755 del_timer_sync(&bcsp->tbcsp);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 hu->priv = NULL;
758
759 BT_DBG("hu %p", hu);
760
761 skb_queue_purge(&bcsp->unack);
762 skb_queue_purge(&bcsp->rel);
763 skb_queue_purge(&bcsp->unrel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Tomas Bortoli578658d2019-05-28 15:42:58 +0200765 if (bcsp->rx_skb) {
766 kfree_skb(bcsp->rx_skb);
767 bcsp->rx_skb = NULL;
768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 kfree(bcsp);
771 return 0;
772}
773
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700774static const struct hci_uart_proto bcsp = {
Marcel Holtmann0372a662005-10-28 19:20:45 +0200775 .id = HCI_UART_BCSP,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700776 .name = "BCSP",
Marcel Holtmann0372a662005-10-28 19:20:45 +0200777 .open = bcsp_open,
778 .close = bcsp_close,
779 .enqueue = bcsp_enqueue,
780 .dequeue = bcsp_dequeue,
781 .recv = bcsp_recv,
782 .flush = bcsp_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783};
784
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300785int __init bcsp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700787 return hci_uart_register_proto(&bcsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300790int __exit bcsp_deinit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 return hci_uart_unregister_proto(&bcsp);
793}
794
Marcel Holtmann20dd6f52005-10-28 19:20:40 +0200795module_param(txcrc, bool, 0644);
796MODULE_PARM_DESC(txcrc, "Transmit CRC with every BCSP packet");
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798module_param(hciextn, bool, 0644);
799MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into BCSP packets");