Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Bluetooth HCI Three-wire UART driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Intel Corporation |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | |
| 28 | #include <net/bluetooth/bluetooth.h> |
| 29 | #include <net/bluetooth/hci_core.h> |
| 30 | |
| 31 | #include "hci_uart.h" |
| 32 | |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 33 | #define HCI_3WIRE_ACK_PKT 0 |
| 34 | #define HCI_3WIRE_LINK_PKT 15 |
| 35 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 36 | #define H5_TXWINSIZE 4 |
| 37 | |
| 38 | #define H5_ACK_TIMEOUT msecs_to_jiffies(250) |
| 39 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 40 | /* |
| 41 | * Maximum Three-wire packet: |
| 42 | * 4 byte header + max value for 12-bit length + 2 bytes for CRC |
| 43 | */ |
| 44 | #define H5_MAX_LEN (4 + 0xfff + 2) |
| 45 | |
Johan Hedberg | 01977c0 | 2012-07-16 16:12:07 +0300 | [diff] [blame^] | 46 | /* Convenience macros for reading Three-wire header values */ |
| 47 | #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07) |
| 48 | #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07) |
| 49 | #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01) |
| 50 | #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01) |
| 51 | #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f) |
| 52 | #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4)) |
| 53 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 54 | #define SLIP_DELIMITER 0xc0 |
| 55 | #define SLIP_ESC 0xdb |
| 56 | #define SLIP_ESC_DELIM 0xdc |
| 57 | #define SLIP_ESC_ESC 0xdd |
| 58 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 59 | struct h5 { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 60 | struct sk_buff_head unack; /* Unack'ed packets queue */ |
| 61 | struct sk_buff_head rel; /* Reliable packets queue */ |
| 62 | struct sk_buff_head unrel; /* Unreliable packets queue */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 63 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 64 | struct sk_buff *rx_skb; /* Receive buffer */ |
| 65 | size_t rx_pending; /* Expecting more bytes */ |
| 66 | bool rx_esc; /* SLIP escape mode */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 67 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 68 | int (*rx_func) (struct hci_uart *hu, u8 c); |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 69 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 70 | struct timer_list timer; /* Retransmission timer */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 71 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 72 | bool txack_req; |
| 73 | |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 74 | u8 next_ack; |
| 75 | u8 next_seq; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 76 | }; |
| 77 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 78 | static void h5_reset_rx(struct h5 *h5); |
| 79 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 80 | static void h5_timed_event(unsigned long arg) |
| 81 | { |
| 82 | struct hci_uart *hu = (struct hci_uart *) arg; |
| 83 | struct h5 *h5 = hu->priv; |
| 84 | struct sk_buff *skb; |
| 85 | unsigned long flags; |
| 86 | |
| 87 | BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen); |
| 88 | |
| 89 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
| 90 | |
| 91 | while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) { |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 92 | h5->next_seq = (h5->next_seq - 1) & 0x07; |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 93 | skb_queue_head(&h5->rel, skb); |
| 94 | } |
| 95 | |
| 96 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 97 | |
| 98 | hci_uart_tx_wakeup(hu); |
| 99 | } |
| 100 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 101 | static int h5_open(struct hci_uart *hu) |
| 102 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 103 | struct h5 *h5; |
| 104 | |
| 105 | BT_DBG("hu %p", hu); |
| 106 | |
| 107 | h5 = kzalloc(sizeof(*h5), GFP_KERNEL); |
| 108 | if (!h5) |
| 109 | return -ENOMEM; |
| 110 | |
| 111 | hu->priv = h5; |
| 112 | |
| 113 | skb_queue_head_init(&h5->unack); |
| 114 | skb_queue_head_init(&h5->rel); |
| 115 | skb_queue_head_init(&h5->unrel); |
| 116 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 117 | h5_reset_rx(h5); |
| 118 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 119 | init_timer(&h5->timer); |
| 120 | h5->timer.function = h5_timed_event; |
| 121 | h5->timer.data = (unsigned long) hu; |
| 122 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 123 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int h5_close(struct hci_uart *hu) |
| 127 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 128 | struct h5 *h5 = hu->priv; |
| 129 | |
| 130 | skb_queue_purge(&h5->unack); |
| 131 | skb_queue_purge(&h5->rel); |
| 132 | skb_queue_purge(&h5->unrel); |
| 133 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 134 | del_timer(&h5->timer); |
| 135 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 136 | kfree(h5); |
| 137 | |
| 138 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 139 | } |
| 140 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 141 | static void h5_handle_internal_rx(struct hci_uart *hu) |
| 142 | { |
| 143 | BT_DBG("%s", hu->hdev->name); |
| 144 | } |
| 145 | |
| 146 | static void h5_complete_rx_pkt(struct hci_uart *hu) |
| 147 | { |
| 148 | struct h5 *h5 = hu->priv; |
| 149 | u8 pkt_type; |
| 150 | |
| 151 | BT_DBG("%s", hu->hdev->name); |
| 152 | |
| 153 | pkt_type = h5->rx_skb->data[1] & 0x0f; |
| 154 | |
| 155 | switch (pkt_type) { |
| 156 | case HCI_EVENT_PKT: |
| 157 | case HCI_ACLDATA_PKT: |
| 158 | case HCI_SCODATA_PKT: |
| 159 | bt_cb(h5->rx_skb)->pkt_type = pkt_type; |
| 160 | |
| 161 | /* Remove Three-wire header */ |
| 162 | skb_pull(h5->rx_skb, 4); |
| 163 | |
| 164 | hci_recv_frame(h5->rx_skb); |
| 165 | h5->rx_skb = NULL; |
| 166 | |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | h5_handle_internal_rx(hu); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | h5_reset_rx(h5); |
| 175 | } |
| 176 | |
| 177 | static int h5_rx_crc(struct hci_uart *hu, unsigned char c) |
| 178 | { |
| 179 | struct h5 *h5 = hu->priv; |
| 180 | |
| 181 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
| 182 | |
| 183 | h5_complete_rx_pkt(hu); |
| 184 | h5_reset_rx(h5); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int h5_rx_payload(struct hci_uart *hu, unsigned char c) |
| 190 | { |
| 191 | struct h5 *h5 = hu->priv; |
| 192 | const unsigned char *hdr = h5->rx_skb->data; |
| 193 | |
| 194 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
| 195 | |
| 196 | if ((hdr[0] >> 4) & 0x01) { |
| 197 | h5->rx_func = h5_rx_crc; |
| 198 | h5->rx_pending = 2; |
| 199 | } else { |
| 200 | h5_complete_rx_pkt(hu); |
| 201 | h5_reset_rx(h5); |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c) |
| 208 | { |
| 209 | struct h5 *h5 = hu->priv; |
| 210 | const unsigned char *hdr = h5->rx_skb->data; |
| 211 | |
| 212 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
| 213 | |
| 214 | if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) { |
| 215 | BT_ERR("Invalid header checksum"); |
| 216 | h5_reset_rx(h5); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | h5->rx_func = h5_rx_payload; |
| 221 | h5->rx_pending = ((hdr[1] >> 4) & 0xff) + (hdr[2] << 4); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c) |
| 227 | { |
| 228 | struct h5 *h5 = hu->priv; |
| 229 | |
| 230 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
| 231 | |
| 232 | if (c == SLIP_DELIMITER) |
| 233 | return 1; |
| 234 | |
| 235 | h5->rx_func = h5_rx_3wire_hdr; |
| 236 | h5->rx_pending = 4; |
| 237 | |
| 238 | h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC); |
| 239 | if (!h5->rx_skb) { |
| 240 | BT_ERR("Can't allocate mem for new packet"); |
| 241 | h5_reset_rx(h5); |
| 242 | return -ENOMEM; |
| 243 | } |
| 244 | |
| 245 | h5->rx_skb->dev = (void *) hu->hdev; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c) |
| 251 | { |
| 252 | struct h5 *h5 = hu->priv; |
| 253 | |
| 254 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
| 255 | |
| 256 | if (c == SLIP_DELIMITER) |
| 257 | h5->rx_func = h5_rx_pkt_start; |
| 258 | |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | static void h5_unslip_one_byte(struct h5 *h5, unsigned char c) |
| 263 | { |
| 264 | const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC; |
| 265 | const u8 *byte = &c; |
| 266 | |
| 267 | if (!h5->rx_esc && c == SLIP_ESC) { |
| 268 | h5->rx_esc = true; |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (h5->rx_esc) { |
| 273 | switch (c) { |
| 274 | case SLIP_ESC_DELIM: |
| 275 | byte = &delim; |
| 276 | break; |
| 277 | case SLIP_ESC_ESC: |
| 278 | byte = &esc; |
| 279 | break; |
| 280 | default: |
| 281 | BT_ERR("Invalid esc byte 0x%02hhx", c); |
| 282 | h5_reset_rx(h5); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | h5->rx_esc = false; |
| 287 | } |
| 288 | |
| 289 | memcpy(skb_put(h5->rx_skb, 1), byte, 1); |
| 290 | h5->rx_pending--; |
| 291 | |
| 292 | BT_DBG("unsliped 0x%02hhx", *byte); |
| 293 | } |
| 294 | |
| 295 | static void h5_reset_rx(struct h5 *h5) |
| 296 | { |
| 297 | if (h5->rx_skb) { |
| 298 | kfree_skb(h5->rx_skb); |
| 299 | h5->rx_skb = NULL; |
| 300 | } |
| 301 | |
| 302 | h5->rx_func = h5_rx_delimiter; |
| 303 | h5->rx_pending = 0; |
| 304 | h5->rx_esc = false; |
| 305 | } |
| 306 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 307 | static int h5_recv(struct hci_uart *hu, void *data, int count) |
| 308 | { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 309 | struct h5 *h5 = hu->priv; |
| 310 | unsigned char *ptr = data; |
| 311 | |
| 312 | BT_DBG("%s count %d", hu->hdev->name, count); |
| 313 | |
| 314 | while (count > 0) { |
| 315 | int processed; |
| 316 | |
| 317 | if (h5->rx_pending > 0) { |
| 318 | if (*ptr == SLIP_DELIMITER) { |
| 319 | BT_ERR("Too short H5 packet"); |
| 320 | h5_reset_rx(h5); |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | h5_unslip_one_byte(h5, *ptr); |
| 325 | |
| 326 | ptr++; count--; |
| 327 | continue; |
| 328 | } |
| 329 | |
| 330 | processed = h5->rx_func(hu, *ptr); |
| 331 | if (processed < 0) |
| 332 | return processed; |
| 333 | |
| 334 | ptr += processed; |
| 335 | count -= processed; |
| 336 | } |
| 337 | |
| 338 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 342 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 343 | struct h5 *h5 = hu->priv; |
| 344 | |
| 345 | if (skb->len > 0xfff) { |
| 346 | BT_ERR("Packet too long (%u bytes)", skb->len); |
| 347 | kfree_skb(skb); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | switch (bt_cb(skb)->pkt_type) { |
| 352 | case HCI_ACLDATA_PKT: |
| 353 | case HCI_COMMAND_PKT: |
| 354 | skb_queue_tail(&h5->rel, skb); |
| 355 | break; |
| 356 | |
| 357 | case HCI_SCODATA_PKT: |
| 358 | skb_queue_tail(&h5->unrel, skb); |
| 359 | break; |
| 360 | |
| 361 | default: |
| 362 | BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type); |
| 363 | kfree_skb(skb); |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 370 | static void h5_slip_delim(struct sk_buff *skb) |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 371 | { |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 372 | const char delim = SLIP_DELIMITER; |
| 373 | |
| 374 | memcpy(skb_put(skb, 1), &delim, 1); |
| 375 | } |
| 376 | |
| 377 | static void h5_slip_one_byte(struct sk_buff *skb, u8 c) |
| 378 | { |
| 379 | const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM }; |
| 380 | const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC }; |
| 381 | |
| 382 | switch (c) { |
| 383 | case SLIP_DELIMITER: |
| 384 | memcpy(skb_put(skb, 2), &esc_delim, 2); |
| 385 | break; |
| 386 | case SLIP_ESC: |
| 387 | memcpy(skb_put(skb, 2), &esc_esc, 2); |
| 388 | break; |
| 389 | default: |
| 390 | memcpy(skb_put(skb, 1), &c, 1); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type, |
| 395 | const u8 *data, size_t len) |
| 396 | { |
| 397 | struct sk_buff *nskb; |
| 398 | u8 hdr[4]; |
| 399 | int i; |
| 400 | |
| 401 | /* |
| 402 | * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2 |
| 403 | * (because bytes 0xc0 and 0xdb are escaped, worst case is when |
| 404 | * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0 |
| 405 | * delimiters at start and end). |
| 406 | */ |
| 407 | nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC); |
| 408 | if (!nskb) |
| 409 | return NULL; |
| 410 | |
| 411 | bt_cb(nskb)->pkt_type = pkt_type; |
| 412 | |
| 413 | h5_slip_delim(nskb); |
| 414 | |
| 415 | hdr[0] = h5->next_ack << 3; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 416 | h5->txack_req = false; |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 417 | |
| 418 | if (rel) { |
| 419 | hdr[0] |= 1 << 7; |
| 420 | hdr[0] |= h5->next_seq; |
| 421 | h5->next_seq = (h5->next_seq + 1) % 8; |
| 422 | } |
| 423 | |
| 424 | hdr[1] = pkt_type | ((len & 0x0f) << 4); |
| 425 | hdr[2] = len >> 4; |
| 426 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); |
| 427 | |
| 428 | for (i = 0; i < 4; i++) |
| 429 | h5_slip_one_byte(nskb, hdr[i]); |
| 430 | |
| 431 | for (i = 0; i < len; i++) |
| 432 | h5_slip_one_byte(nskb, data[i]); |
| 433 | |
| 434 | h5_slip_delim(nskb); |
| 435 | |
| 436 | return nskb; |
| 437 | } |
| 438 | |
| 439 | static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type, |
| 440 | const u8 *data, size_t len) |
| 441 | { |
| 442 | bool rel; |
| 443 | |
| 444 | switch (pkt_type) { |
| 445 | case HCI_ACLDATA_PKT: |
| 446 | case HCI_COMMAND_PKT: |
| 447 | rel = true; |
| 448 | break; |
| 449 | case HCI_SCODATA_PKT: |
| 450 | case HCI_3WIRE_LINK_PKT: |
| 451 | case HCI_3WIRE_ACK_PKT: |
| 452 | rel = false; |
| 453 | break; |
| 454 | default: |
| 455 | BT_ERR("Unknown packet type %u", pkt_type); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | return h5_build_pkt(h5, rel, pkt_type, data, len); |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static struct sk_buff *h5_prepare_ack(struct h5 *h5) |
| 463 | { |
| 464 | h5->txack_req = false; |
| 465 | return NULL; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) |
| 469 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 470 | struct h5 *h5 = hu->priv; |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 471 | unsigned long flags; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 472 | struct sk_buff *skb, *nskb; |
| 473 | |
| 474 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 475 | nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type, |
| 476 | skb->data, skb->len); |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 477 | if (nskb) { |
| 478 | kfree_skb(skb); |
| 479 | return nskb; |
| 480 | } |
| 481 | |
| 482 | skb_queue_head(&h5->unrel, skb); |
| 483 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); |
| 484 | } |
| 485 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 486 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
| 487 | |
| 488 | if (h5->unack.qlen >= H5_TXWINSIZE) |
| 489 | goto unlock; |
| 490 | |
| 491 | if ((skb = skb_dequeue(&h5->rel)) != NULL) { |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 492 | nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type, |
| 493 | skb->data, skb->len); |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 494 | if (nskb) { |
| 495 | __skb_queue_tail(&h5->unack, skb); |
| 496 | mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT); |
| 497 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 498 | return nskb; |
| 499 | } |
| 500 | |
| 501 | skb_queue_head(&h5->rel, skb); |
| 502 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); |
| 503 | } |
| 504 | |
| 505 | unlock: |
| 506 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 507 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 508 | if (h5->txack_req) |
| 509 | return h5_prepare_ack(h5); |
| 510 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | static int h5_flush(struct hci_uart *hu) |
| 515 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 516 | BT_DBG("hu %p", hu); |
| 517 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static struct hci_uart_proto h5p = { |
| 521 | .id = HCI_UART_3WIRE, |
| 522 | .open = h5_open, |
| 523 | .close = h5_close, |
| 524 | .recv = h5_recv, |
| 525 | .enqueue = h5_enqueue, |
| 526 | .dequeue = h5_dequeue, |
| 527 | .flush = h5_flush, |
| 528 | }; |
| 529 | |
| 530 | int __init h5_init(void) |
| 531 | { |
| 532 | int err = hci_uart_register_proto(&h5p); |
| 533 | |
| 534 | if (!err) |
| 535 | BT_INFO("HCI Three-wire UART (H5) protocol initialized"); |
| 536 | else |
| 537 | BT_ERR("HCI Three-wire UART (H5) protocol init failed"); |
| 538 | |
| 539 | return err; |
| 540 | } |
| 541 | |
| 542 | int __exit h5_deinit(void) |
| 543 | { |
| 544 | return hci_uart_unregister_proto(&h5p); |
| 545 | } |