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