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