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) |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 39 | #define H5_SYNC_TIMEOUT msecs_to_jiffies(100) |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 40 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 41 | /* |
| 42 | * Maximum Three-wire packet: |
| 43 | * 4 byte header + max value for 12-bit length + 2 bytes for CRC |
| 44 | */ |
| 45 | #define H5_MAX_LEN (4 + 0xfff + 2) |
| 46 | |
Johan Hedberg | 01977c0 | 2012-07-16 16:12:07 +0300 | [diff] [blame] | 47 | /* Convenience macros for reading Three-wire header values */ |
| 48 | #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07) |
| 49 | #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07) |
| 50 | #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01) |
| 51 | #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01) |
| 52 | #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f) |
| 53 | #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4)) |
| 54 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 55 | #define SLIP_DELIMITER 0xc0 |
| 56 | #define SLIP_ESC 0xdb |
| 57 | #define SLIP_ESC_DELIM 0xdc |
| 58 | #define SLIP_ESC_ESC 0xdd |
| 59 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 60 | struct h5 { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 61 | struct sk_buff_head unack; /* Unack'ed packets queue */ |
| 62 | struct sk_buff_head rel; /* Reliable packets queue */ |
| 63 | struct sk_buff_head unrel; /* Unreliable packets queue */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 64 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 65 | struct sk_buff *rx_skb; /* Receive buffer */ |
| 66 | size_t rx_pending; /* Expecting more bytes */ |
| 67 | bool rx_esc; /* SLIP escape mode */ |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 68 | u8 rx_ack; /* Last ack number received */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 69 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 70 | int (*rx_func) (struct hci_uart *hu, u8 c); |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 71 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 72 | struct timer_list timer; /* Retransmission timer */ |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 73 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 74 | bool tx_ack_req; /* Pending ack to send */ |
| 75 | u8 tx_seq; /* Next seq number to send */ |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 76 | u8 tx_ack; /* Next ack number to send */ |
Johan Hedberg | 10122d0 | 2012-07-16 16:12:14 +0300 | [diff] [blame] | 77 | |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 78 | enum { |
| 79 | H5_UNINITIALIZED, |
| 80 | H5_INITIALIZED, |
| 81 | H5_ACTIVE, |
| 82 | } state; |
| 83 | |
Johan Hedberg | 10122d0 | 2012-07-16 16:12:14 +0300 | [diff] [blame] | 84 | bool sleeping; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 85 | }; |
| 86 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 87 | static void h5_reset_rx(struct h5 *h5); |
| 88 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 89 | static void h5_link_control(struct hci_uart *hu, const void *data, size_t len) |
| 90 | { |
| 91 | struct h5 *h5 = hu->priv; |
| 92 | struct sk_buff *nskb; |
| 93 | |
| 94 | nskb = alloc_skb(3, GFP_ATOMIC); |
| 95 | if (!nskb) |
| 96 | return; |
| 97 | |
| 98 | bt_cb(nskb)->pkt_type = HCI_3WIRE_LINK_PKT; |
| 99 | |
| 100 | memcpy(skb_put(nskb, len), data, len); |
| 101 | |
| 102 | skb_queue_tail(&h5->unrel, nskb); |
| 103 | } |
| 104 | |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 105 | static void h5_timed_event(unsigned long arg) |
| 106 | { |
| 107 | const unsigned char sync_req[] = { 0x01, 0x7e }; |
| 108 | const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 }; |
| 109 | struct hci_uart *hu = (struct hci_uart *) arg; |
| 110 | struct h5 *h5 = hu->priv; |
| 111 | struct sk_buff *skb; |
| 112 | unsigned long flags; |
| 113 | |
| 114 | if (h5->state == H5_UNINITIALIZED) |
| 115 | h5_link_control(hu, sync_req, sizeof(sync_req)); |
| 116 | |
| 117 | if (h5->state == H5_INITIALIZED) |
| 118 | h5_link_control(hu, conf_req, sizeof(conf_req)); |
| 119 | |
| 120 | if (h5->state != H5_ACTIVE) { |
| 121 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); |
| 122 | goto wakeup; |
| 123 | } |
| 124 | |
| 125 | BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen); |
| 126 | |
| 127 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
| 128 | |
| 129 | while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) { |
| 130 | h5->tx_seq = (h5->tx_seq - 1) & 0x07; |
| 131 | skb_queue_head(&h5->rel, skb); |
| 132 | } |
| 133 | |
| 134 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 135 | |
| 136 | wakeup: |
| 137 | hci_uart_tx_wakeup(hu); |
| 138 | } |
| 139 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 140 | static int h5_open(struct hci_uart *hu) |
| 141 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 142 | struct h5 *h5; |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 143 | const unsigned char sync[] = { 0x01, 0x7e }; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 144 | |
| 145 | BT_DBG("hu %p", hu); |
| 146 | |
| 147 | h5 = kzalloc(sizeof(*h5), GFP_KERNEL); |
| 148 | if (!h5) |
| 149 | return -ENOMEM; |
| 150 | |
| 151 | hu->priv = h5; |
| 152 | |
| 153 | skb_queue_head_init(&h5->unack); |
| 154 | skb_queue_head_init(&h5->rel); |
| 155 | skb_queue_head_init(&h5->unrel); |
| 156 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 157 | h5_reset_rx(h5); |
| 158 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 159 | init_timer(&h5->timer); |
| 160 | h5->timer.function = h5_timed_event; |
| 161 | h5->timer.data = (unsigned long) hu; |
| 162 | |
Johan Hedberg | cd1b442 | 2012-07-16 16:12:12 +0300 | [diff] [blame] | 163 | set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags); |
| 164 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 165 | /* Send initial sync request */ |
| 166 | h5_link_control(hu, sync, sizeof(sync)); |
| 167 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); |
| 168 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 169 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static int h5_close(struct hci_uart *hu) |
| 173 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 174 | struct h5 *h5 = hu->priv; |
| 175 | |
| 176 | skb_queue_purge(&h5->unack); |
| 177 | skb_queue_purge(&h5->rel); |
| 178 | skb_queue_purge(&h5->unrel); |
| 179 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 180 | del_timer(&h5->timer); |
| 181 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 182 | kfree(h5); |
| 183 | |
| 184 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 185 | } |
| 186 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 187 | static void h5_pkt_cull(struct h5 *h5) |
| 188 | { |
| 189 | struct sk_buff *skb, *tmp; |
| 190 | unsigned long flags; |
| 191 | int i, to_remove; |
| 192 | u8 seq; |
| 193 | |
| 194 | spin_lock_irqsave(&h5->unack.lock, flags); |
| 195 | |
| 196 | to_remove = skb_queue_len(&h5->unack); |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 197 | if (to_remove == 0) |
| 198 | goto unlock; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 199 | |
| 200 | seq = h5->tx_seq; |
| 201 | |
| 202 | while (to_remove > 0) { |
| 203 | if (h5->rx_ack == seq) |
| 204 | break; |
| 205 | |
| 206 | to_remove--; |
| 207 | seq = (seq - 1) % 8; |
| 208 | } |
| 209 | |
| 210 | if (seq != h5->rx_ack) |
| 211 | BT_ERR("Controller acked invalid packet"); |
| 212 | |
| 213 | i = 0; |
| 214 | skb_queue_walk_safe(&h5->unack, skb, tmp) { |
| 215 | if (i++ >= to_remove) |
| 216 | break; |
| 217 | |
| 218 | __skb_unlink(skb, &h5->unack); |
| 219 | kfree_skb(skb); |
| 220 | } |
| 221 | |
| 222 | if (skb_queue_empty(&h5->unack)) |
| 223 | del_timer(&h5->timer); |
| 224 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 225 | unlock: |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 226 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 227 | } |
| 228 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 229 | static void h5_handle_internal_rx(struct hci_uart *hu) |
| 230 | { |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 231 | struct h5 *h5 = hu->priv; |
| 232 | const unsigned char sync_req[] = { 0x01, 0x7e }; |
| 233 | const unsigned char sync_rsp[] = { 0x02, 0x7d }; |
| 234 | const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 }; |
| 235 | const unsigned char conf_rsp[] = { 0x04, 0x7b, 0x01 }; |
Johan Hedberg | 10122d0 | 2012-07-16 16:12:14 +0300 | [diff] [blame] | 236 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; |
| 237 | const unsigned char woken_req[] = { 0x06, 0xf9 }; |
| 238 | const unsigned char sleep_req[] = { 0x07, 0x78 }; |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 239 | const unsigned char *hdr = h5->rx_skb->data; |
| 240 | const unsigned char *data = &h5->rx_skb->data[4]; |
| 241 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 242 | BT_DBG("%s", hu->hdev->name); |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 243 | |
| 244 | if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) |
| 245 | return; |
| 246 | |
| 247 | if (H5_HDR_LEN(hdr) < 2) |
| 248 | return; |
| 249 | |
| 250 | if (memcmp(data, sync_req, 2) == 0) { |
| 251 | h5_link_control(hu, sync_rsp, 2); |
| 252 | } else if (memcmp(data, sync_rsp, 2) == 0) { |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 253 | h5->state = H5_INITIALIZED; |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 254 | h5_link_control(hu, conf_req, 3); |
| 255 | } else if (memcmp(data, conf_req, 2) == 0) { |
| 256 | h5_link_control(hu, conf_rsp, 2); |
| 257 | h5_link_control(hu, conf_req, 3); |
| 258 | } else if (memcmp(data, conf_rsp, 2) == 0) { |
| 259 | BT_DBG("Three-wire init sequence complete"); |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 260 | h5->state = H5_ACTIVE; |
Johan Hedberg | cd1b442 | 2012-07-16 16:12:12 +0300 | [diff] [blame] | 261 | hci_uart_init_ready(hu); |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 262 | return; |
Johan Hedberg | 10122d0 | 2012-07-16 16:12:14 +0300 | [diff] [blame] | 263 | } else if (memcmp(data, sleep_req, 2) == 0) { |
| 264 | BT_DBG("Peer went to sleep"); |
| 265 | h5->sleeping = true; |
| 266 | h5_link_control(hu, wakeup_req, 2); |
| 267 | } else if (memcmp(data, woken_req, 2) == 0) { |
| 268 | BT_DBG("Peer woke up"); |
| 269 | h5->sleeping = false; |
| 270 | return; |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 271 | } else { |
| 272 | BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | hci_uart_tx_wakeup(hu); |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void h5_complete_rx_pkt(struct hci_uart *hu) |
| 280 | { |
| 281 | struct h5 *h5 = hu->priv; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 282 | const unsigned char *hdr = h5->rx_skb->data; |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 283 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 284 | if (H5_HDR_RELIABLE(hdr)) { |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 285 | h5->tx_ack = (h5->tx_ack + 1) % 8; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 286 | h5->tx_ack_req = true; |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 287 | hci_uart_tx_wakeup(hu); |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 288 | } |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 289 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 290 | h5->rx_ack = H5_HDR_ACK(hdr); |
| 291 | |
| 292 | h5_pkt_cull(h5); |
| 293 | |
| 294 | switch (H5_HDR_PKT_TYPE(hdr)) { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 295 | case HCI_EVENT_PKT: |
| 296 | case HCI_ACLDATA_PKT: |
| 297 | case HCI_SCODATA_PKT: |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 298 | bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr); |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 299 | |
| 300 | /* Remove Three-wire header */ |
| 301 | skb_pull(h5->rx_skb, 4); |
| 302 | |
| 303 | hci_recv_frame(h5->rx_skb); |
| 304 | h5->rx_skb = NULL; |
| 305 | |
| 306 | break; |
| 307 | |
| 308 | default: |
| 309 | h5_handle_internal_rx(hu); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | h5_reset_rx(h5); |
| 314 | } |
| 315 | |
| 316 | static int h5_rx_crc(struct hci_uart *hu, unsigned char c) |
| 317 | { |
| 318 | struct h5 *h5 = hu->priv; |
| 319 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 320 | h5_complete_rx_pkt(hu); |
| 321 | h5_reset_rx(h5); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int h5_rx_payload(struct hci_uart *hu, unsigned char c) |
| 327 | { |
| 328 | struct h5 *h5 = hu->priv; |
| 329 | const unsigned char *hdr = h5->rx_skb->data; |
| 330 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 331 | if (H5_HDR_CRC(hdr)) { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 332 | h5->rx_func = h5_rx_crc; |
| 333 | h5->rx_pending = 2; |
| 334 | } else { |
| 335 | h5_complete_rx_pkt(hu); |
| 336 | h5_reset_rx(h5); |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c) |
| 343 | { |
| 344 | struct h5 *h5 = hu->priv; |
| 345 | const unsigned char *hdr = h5->rx_skb->data; |
| 346 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 347 | BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u", |
| 348 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), |
| 349 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), |
| 350 | H5_HDR_LEN(hdr)); |
| 351 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 352 | if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) { |
| 353 | BT_ERR("Invalid header checksum"); |
| 354 | h5_reset_rx(h5); |
| 355 | return 0; |
| 356 | } |
| 357 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 358 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) { |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 359 | BT_ERR("Out-of-order packet arrived (%u != %u)", |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 360 | H5_HDR_SEQ(hdr), h5->tx_ack); |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 361 | h5_reset_rx(h5); |
| 362 | return 0; |
| 363 | } |
| 364 | |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 365 | if (h5->state != H5_ACTIVE && |
| 366 | H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) { |
| 367 | BT_ERR("Non-link packet received in non-active state"); |
| 368 | h5_reset_rx(h5); |
| 369 | } |
| 370 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 371 | h5->rx_func = h5_rx_payload; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 372 | h5->rx_pending = H5_HDR_LEN(hdr); |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c) |
| 378 | { |
| 379 | struct h5 *h5 = hu->priv; |
| 380 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 381 | if (c == SLIP_DELIMITER) |
| 382 | return 1; |
| 383 | |
| 384 | h5->rx_func = h5_rx_3wire_hdr; |
| 385 | h5->rx_pending = 4; |
| 386 | |
| 387 | h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC); |
| 388 | if (!h5->rx_skb) { |
| 389 | BT_ERR("Can't allocate mem for new packet"); |
| 390 | h5_reset_rx(h5); |
| 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | h5->rx_skb->dev = (void *) hu->hdev; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c) |
| 400 | { |
| 401 | struct h5 *h5 = hu->priv; |
| 402 | |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 403 | if (c == SLIP_DELIMITER) |
| 404 | h5->rx_func = h5_rx_pkt_start; |
| 405 | |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | static void h5_unslip_one_byte(struct h5 *h5, unsigned char c) |
| 410 | { |
| 411 | const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC; |
| 412 | const u8 *byte = &c; |
| 413 | |
| 414 | if (!h5->rx_esc && c == SLIP_ESC) { |
| 415 | h5->rx_esc = true; |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | if (h5->rx_esc) { |
| 420 | switch (c) { |
| 421 | case SLIP_ESC_DELIM: |
| 422 | byte = &delim; |
| 423 | break; |
| 424 | case SLIP_ESC_ESC: |
| 425 | byte = &esc; |
| 426 | break; |
| 427 | default: |
| 428 | BT_ERR("Invalid esc byte 0x%02hhx", c); |
| 429 | h5_reset_rx(h5); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | h5->rx_esc = false; |
| 434 | } |
| 435 | |
| 436 | memcpy(skb_put(h5->rx_skb, 1), byte, 1); |
| 437 | h5->rx_pending--; |
| 438 | |
Johan Hedberg | 255a68e | 2012-07-16 16:12:13 +0300 | [diff] [blame] | 439 | BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending); |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void h5_reset_rx(struct h5 *h5) |
| 443 | { |
| 444 | if (h5->rx_skb) { |
| 445 | kfree_skb(h5->rx_skb); |
| 446 | h5->rx_skb = NULL; |
| 447 | } |
| 448 | |
| 449 | h5->rx_func = h5_rx_delimiter; |
| 450 | h5->rx_pending = 0; |
| 451 | h5->rx_esc = false; |
| 452 | } |
| 453 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 454 | static int h5_recv(struct hci_uart *hu, void *data, int count) |
| 455 | { |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 456 | struct h5 *h5 = hu->priv; |
| 457 | unsigned char *ptr = data; |
| 458 | |
Johan Hedberg | 255a68e | 2012-07-16 16:12:13 +0300 | [diff] [blame] | 459 | BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending, |
| 460 | count); |
Johan Hedberg | bc1f35b | 2012-07-16 16:12:05 +0300 | [diff] [blame] | 461 | |
| 462 | while (count > 0) { |
| 463 | int processed; |
| 464 | |
| 465 | if (h5->rx_pending > 0) { |
| 466 | if (*ptr == SLIP_DELIMITER) { |
| 467 | BT_ERR("Too short H5 packet"); |
| 468 | h5_reset_rx(h5); |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | h5_unslip_one_byte(h5, *ptr); |
| 473 | |
| 474 | ptr++; count--; |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | processed = h5->rx_func(hu, *ptr); |
| 479 | if (processed < 0) |
| 480 | return processed; |
| 481 | |
| 482 | ptr += processed; |
| 483 | count -= processed; |
| 484 | } |
| 485 | |
| 486 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 490 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 491 | struct h5 *h5 = hu->priv; |
| 492 | |
| 493 | if (skb->len > 0xfff) { |
| 494 | BT_ERR("Packet too long (%u bytes)", skb->len); |
| 495 | kfree_skb(skb); |
| 496 | return 0; |
| 497 | } |
| 498 | |
Johan Hedberg | f674a05 | 2012-07-16 16:12:15 +0300 | [diff] [blame^] | 499 | if (h5->state != H5_ACTIVE) { |
| 500 | BT_ERR("Ignoring HCI data in non-active state"); |
| 501 | kfree_skb(skb); |
| 502 | return 0; |
| 503 | } |
| 504 | |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 505 | switch (bt_cb(skb)->pkt_type) { |
| 506 | case HCI_ACLDATA_PKT: |
| 507 | case HCI_COMMAND_PKT: |
| 508 | skb_queue_tail(&h5->rel, skb); |
| 509 | break; |
| 510 | |
| 511 | case HCI_SCODATA_PKT: |
| 512 | skb_queue_tail(&h5->unrel, skb); |
| 513 | break; |
| 514 | |
| 515 | default: |
| 516 | BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type); |
| 517 | kfree_skb(skb); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 524 | static void h5_slip_delim(struct sk_buff *skb) |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 525 | { |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 526 | const char delim = SLIP_DELIMITER; |
| 527 | |
| 528 | memcpy(skb_put(skb, 1), &delim, 1); |
| 529 | } |
| 530 | |
| 531 | static void h5_slip_one_byte(struct sk_buff *skb, u8 c) |
| 532 | { |
| 533 | const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM }; |
| 534 | const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC }; |
| 535 | |
| 536 | switch (c) { |
| 537 | case SLIP_DELIMITER: |
| 538 | memcpy(skb_put(skb, 2), &esc_delim, 2); |
| 539 | break; |
| 540 | case SLIP_ESC: |
| 541 | memcpy(skb_put(skb, 2), &esc_esc, 2); |
| 542 | break; |
| 543 | default: |
| 544 | memcpy(skb_put(skb, 1), &c, 1); |
| 545 | } |
| 546 | } |
| 547 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 548 | static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type, |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 549 | const u8 *data, size_t len) |
| 550 | { |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 551 | struct h5 *h5 = hu->priv; |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 552 | struct sk_buff *nskb; |
| 553 | u8 hdr[4]; |
| 554 | int i; |
| 555 | |
| 556 | /* |
| 557 | * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2 |
| 558 | * (because bytes 0xc0 and 0xdb are escaped, worst case is when |
| 559 | * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0 |
| 560 | * delimiters at start and end). |
| 561 | */ |
| 562 | nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC); |
| 563 | if (!nskb) |
| 564 | return NULL; |
| 565 | |
| 566 | bt_cb(nskb)->pkt_type = pkt_type; |
| 567 | |
| 568 | h5_slip_delim(nskb); |
| 569 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 570 | hdr[0] = h5->tx_ack << 3; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 571 | h5->tx_ack_req = false; |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 572 | |
| 573 | if (rel) { |
| 574 | hdr[0] |= 1 << 7; |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 575 | hdr[0] |= h5->tx_seq; |
| 576 | h5->tx_seq = (h5->tx_seq + 1) % 8; |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | hdr[1] = pkt_type | ((len & 0x0f) << 4); |
| 580 | hdr[2] = len >> 4; |
| 581 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); |
| 582 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 583 | BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u", |
| 584 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), |
| 585 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), |
| 586 | H5_HDR_LEN(hdr)); |
| 587 | |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 588 | for (i = 0; i < 4; i++) |
| 589 | h5_slip_one_byte(nskb, hdr[i]); |
| 590 | |
| 591 | for (i = 0; i < len; i++) |
| 592 | h5_slip_one_byte(nskb, data[i]); |
| 593 | |
| 594 | h5_slip_delim(nskb); |
| 595 | |
| 596 | return nskb; |
| 597 | } |
| 598 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 599 | static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type, |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 600 | const u8 *data, size_t len) |
| 601 | { |
| 602 | bool rel; |
| 603 | |
| 604 | switch (pkt_type) { |
| 605 | case HCI_ACLDATA_PKT: |
| 606 | case HCI_COMMAND_PKT: |
| 607 | rel = true; |
| 608 | break; |
| 609 | case HCI_SCODATA_PKT: |
| 610 | case HCI_3WIRE_LINK_PKT: |
| 611 | case HCI_3WIRE_ACK_PKT: |
| 612 | rel = false; |
| 613 | break; |
| 614 | default: |
| 615 | BT_ERR("Unknown packet type %u", pkt_type); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 619 | return h5_build_pkt(hu, rel, pkt_type, data, len); |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) |
| 623 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 624 | struct h5 *h5 = hu->priv; |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 625 | unsigned long flags; |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 626 | struct sk_buff *skb, *nskb; |
| 627 | |
| 628 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 629 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 630 | skb->data, skb->len); |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 631 | if (nskb) { |
| 632 | kfree_skb(skb); |
| 633 | return nskb; |
| 634 | } |
| 635 | |
| 636 | skb_queue_head(&h5->unrel, skb); |
| 637 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); |
| 638 | } |
| 639 | |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 640 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
| 641 | |
| 642 | if (h5->unack.qlen >= H5_TXWINSIZE) |
| 643 | goto unlock; |
| 644 | |
| 645 | if ((skb = skb_dequeue(&h5->rel)) != NULL) { |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 646 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
Johan Hedberg | c0a1b73 | 2012-07-16 16:12:06 +0300 | [diff] [blame] | 647 | skb->data, skb->len); |
Johan Hedberg | 3f27e95 | 2012-07-16 16:12:04 +0300 | [diff] [blame] | 648 | if (nskb) { |
| 649 | __skb_queue_tail(&h5->unack, skb); |
| 650 | mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT); |
| 651 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 652 | return nskb; |
| 653 | } |
| 654 | |
| 655 | skb_queue_head(&h5->rel, skb); |
| 656 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); |
| 657 | } |
| 658 | |
| 659 | unlock: |
| 660 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
| 661 | |
Johan Hedberg | 43eb12d | 2012-07-16 16:12:08 +0300 | [diff] [blame] | 662 | if (h5->tx_ack_req) |
Johan Hedberg | 40f1022 | 2012-07-16 16:12:09 +0300 | [diff] [blame] | 663 | return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0); |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 664 | |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | static int h5_flush(struct hci_uart *hu) |
| 669 | { |
Johan Hedberg | 7d664fb | 2012-07-16 16:12:03 +0300 | [diff] [blame] | 670 | BT_DBG("hu %p", hu); |
| 671 | return 0; |
Johan Hedberg | 7dec65c | 2012-07-16 16:12:02 +0300 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static struct hci_uart_proto h5p = { |
| 675 | .id = HCI_UART_3WIRE, |
| 676 | .open = h5_open, |
| 677 | .close = h5_close, |
| 678 | .recv = h5_recv, |
| 679 | .enqueue = h5_enqueue, |
| 680 | .dequeue = h5_dequeue, |
| 681 | .flush = h5_flush, |
| 682 | }; |
| 683 | |
| 684 | int __init h5_init(void) |
| 685 | { |
| 686 | int err = hci_uart_register_proto(&h5p); |
| 687 | |
| 688 | if (!err) |
| 689 | BT_INFO("HCI Three-wire UART (H5) protocol initialized"); |
| 690 | else |
| 691 | BT_ERR("HCI Three-wire UART (H5) protocol init failed"); |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | int __exit h5_deinit(void) |
| 697 | { |
| 698 | return hci_uart_unregister_proto(&h5p); |
| 699 | } |