blob: 831ccfecc8a9650b206f2c6c6e78de5b5948ef90 [file] [log] [blame]
Johan Hedberg7dec65c2012-07-16 16:12:02 +03001/*
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 Hedbergc0a1b732012-07-16 16:12:06 +030033#define HCI_3WIRE_ACK_PKT 0
34#define HCI_3WIRE_LINK_PKT 15
35
Johan Hedbergafdc9442012-07-16 16:12:18 +030036/* Sliding window size */
37#define H5_TX_WIN_MAX 4
Johan Hedberg3f27e952012-07-16 16:12:04 +030038
39#define H5_ACK_TIMEOUT msecs_to_jiffies(250)
Johan Hedberg40f10222012-07-16 16:12:09 +030040#define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
Johan Hedberg3f27e952012-07-16 16:12:04 +030041
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030042/*
43 * Maximum Three-wire packet:
44 * 4 byte header + max value for 12-bit length + 2 bytes for CRC
45 */
46#define H5_MAX_LEN (4 + 0xfff + 2)
47
Johan Hedberg01977c02012-07-16 16:12:07 +030048/* Convenience macros for reading Three-wire header values */
49#define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
50#define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
51#define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
52#define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
53#define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
54#define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4))
55
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030056#define SLIP_DELIMITER 0xc0
57#define SLIP_ESC 0xdb
58#define SLIP_ESC_DELIM 0xdc
59#define SLIP_ESC_ESC 0xdd
60
Johan Hedberg7d664fb2012-07-16 16:12:03 +030061struct h5 {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030062 struct sk_buff_head unack; /* Unack'ed packets queue */
63 struct sk_buff_head rel; /* Reliable packets queue */
64 struct sk_buff_head unrel; /* Unreliable packets queue */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030065
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030066 struct sk_buff *rx_skb; /* Receive buffer */
67 size_t rx_pending; /* Expecting more bytes */
68 bool rx_esc; /* SLIP escape mode */
Johan Hedberg43eb12d2012-07-16 16:12:08 +030069 u8 rx_ack; /* Last ack number received */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030070
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030071 int (*rx_func) (struct hci_uart *hu, u8 c);
Johan Hedberg3f27e952012-07-16 16:12:04 +030072
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030073 struct timer_list timer; /* Retransmission timer */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030074
Johan Hedberg43eb12d2012-07-16 16:12:08 +030075 bool tx_ack_req; /* Pending ack to send */
76 u8 tx_seq; /* Next seq number to send */
Johan Hedberg40f10222012-07-16 16:12:09 +030077 u8 tx_ack; /* Next ack number to send */
Johan Hedbergafdc9442012-07-16 16:12:18 +030078 u8 tx_win; /* Sliding window size */
Johan Hedberg10122d02012-07-16 16:12:14 +030079
Johan Hedbergf674a052012-07-16 16:12:15 +030080 enum {
81 H5_UNINITIALIZED,
82 H5_INITIALIZED,
83 H5_ACTIVE,
84 } state;
85
Johan Hedberg95c5c222012-07-16 16:12:16 +030086 enum {
87 H5_AWAKE,
88 H5_SLEEPING,
89 H5_WAKING_UP,
90 } sleep;
Johan Hedberg7d664fb2012-07-16 16:12:03 +030091};
92
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030093static void h5_reset_rx(struct h5 *h5);
94
Johan Hedberg40f10222012-07-16 16:12:09 +030095static void h5_link_control(struct hci_uart *hu, const void *data, size_t len)
96{
97 struct h5 *h5 = hu->priv;
98 struct sk_buff *nskb;
99
100 nskb = alloc_skb(3, GFP_ATOMIC);
101 if (!nskb)
102 return;
103
104 bt_cb(nskb)->pkt_type = HCI_3WIRE_LINK_PKT;
105
106 memcpy(skb_put(nskb, len), data, len);
107
108 skb_queue_tail(&h5->unrel, nskb);
109}
110
Johan Hedbergafdc9442012-07-16 16:12:18 +0300111static u8 h5_cfg_field(struct h5 *h5)
112{
113 u8 field = 0;
114
115 /* Sliding window size (first 3 bits) */
116 field |= (h5->tx_win & 7);
117
118 return field;
119}
120
Johan Hedbergf674a052012-07-16 16:12:15 +0300121static void h5_timed_event(unsigned long arg)
122{
123 const unsigned char sync_req[] = { 0x01, 0x7e };
Johan Hedbergafdc9442012-07-16 16:12:18 +0300124 unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
Johan Hedbergf674a052012-07-16 16:12:15 +0300125 struct hci_uart *hu = (struct hci_uart *) arg;
126 struct h5 *h5 = hu->priv;
127 struct sk_buff *skb;
128 unsigned long flags;
129
Johan Hedberg95c5c222012-07-16 16:12:16 +0300130 BT_DBG("%s", hu->hdev->name);
131
Johan Hedbergf674a052012-07-16 16:12:15 +0300132 if (h5->state == H5_UNINITIALIZED)
133 h5_link_control(hu, sync_req, sizeof(sync_req));
134
Johan Hedbergafdc9442012-07-16 16:12:18 +0300135 if (h5->state == H5_INITIALIZED) {
136 conf_req[2] = h5_cfg_field(h5);
Johan Hedbergf674a052012-07-16 16:12:15 +0300137 h5_link_control(hu, conf_req, sizeof(conf_req));
Johan Hedbergafdc9442012-07-16 16:12:18 +0300138 }
Johan Hedbergf674a052012-07-16 16:12:15 +0300139
140 if (h5->state != H5_ACTIVE) {
141 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
142 goto wakeup;
143 }
144
Johan Hedberg95c5c222012-07-16 16:12:16 +0300145 if (h5->sleep != H5_AWAKE) {
146 h5->sleep = H5_SLEEPING;
147 goto wakeup;
148 }
149
Johan Hedbergf674a052012-07-16 16:12:15 +0300150 BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
151
152 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
153
154 while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
155 h5->tx_seq = (h5->tx_seq - 1) & 0x07;
156 skb_queue_head(&h5->rel, skb);
157 }
158
159 spin_unlock_irqrestore(&h5->unack.lock, flags);
160
161wakeup:
162 hci_uart_tx_wakeup(hu);
163}
164
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300165static int h5_open(struct hci_uart *hu)
166{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300167 struct h5 *h5;
Johan Hedberg40f10222012-07-16 16:12:09 +0300168 const unsigned char sync[] = { 0x01, 0x7e };
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300169
170 BT_DBG("hu %p", hu);
171
172 h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
173 if (!h5)
174 return -ENOMEM;
175
176 hu->priv = h5;
177
178 skb_queue_head_init(&h5->unack);
179 skb_queue_head_init(&h5->rel);
180 skb_queue_head_init(&h5->unrel);
181
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300182 h5_reset_rx(h5);
183
Johan Hedberg3f27e952012-07-16 16:12:04 +0300184 init_timer(&h5->timer);
185 h5->timer.function = h5_timed_event;
186 h5->timer.data = (unsigned long) hu;
187
Johan Hedbergafdc9442012-07-16 16:12:18 +0300188 h5->tx_win = H5_TX_WIN_MAX;
189
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300190 set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
191
Johan Hedberg40f10222012-07-16 16:12:09 +0300192 /* Send initial sync request */
193 h5_link_control(hu, sync, sizeof(sync));
194 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
195
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300196 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300197}
198
199static int h5_close(struct hci_uart *hu)
200{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300201 struct h5 *h5 = hu->priv;
202
203 skb_queue_purge(&h5->unack);
204 skb_queue_purge(&h5->rel);
205 skb_queue_purge(&h5->unrel);
206
Johan Hedberg3f27e952012-07-16 16:12:04 +0300207 del_timer(&h5->timer);
208
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300209 kfree(h5);
210
211 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300212}
213
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300214static void h5_pkt_cull(struct h5 *h5)
215{
216 struct sk_buff *skb, *tmp;
217 unsigned long flags;
218 int i, to_remove;
219 u8 seq;
220
221 spin_lock_irqsave(&h5->unack.lock, flags);
222
223 to_remove = skb_queue_len(&h5->unack);
Johan Hedberg40f10222012-07-16 16:12:09 +0300224 if (to_remove == 0)
225 goto unlock;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300226
227 seq = h5->tx_seq;
228
229 while (to_remove > 0) {
230 if (h5->rx_ack == seq)
231 break;
232
233 to_remove--;
234 seq = (seq - 1) % 8;
235 }
236
237 if (seq != h5->rx_ack)
238 BT_ERR("Controller acked invalid packet");
239
240 i = 0;
241 skb_queue_walk_safe(&h5->unack, skb, tmp) {
242 if (i++ >= to_remove)
243 break;
244
245 __skb_unlink(skb, &h5->unack);
246 kfree_skb(skb);
247 }
248
249 if (skb_queue_empty(&h5->unack))
250 del_timer(&h5->timer);
251
Johan Hedberg40f10222012-07-16 16:12:09 +0300252unlock:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300253 spin_unlock_irqrestore(&h5->unack.lock, flags);
254}
255
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300256static void h5_handle_internal_rx(struct hci_uart *hu)
257{
Johan Hedberg40f10222012-07-16 16:12:09 +0300258 struct h5 *h5 = hu->priv;
259 const unsigned char sync_req[] = { 0x01, 0x7e };
260 const unsigned char sync_rsp[] = { 0x02, 0x7d };
Johan Hedbergafdc9442012-07-16 16:12:18 +0300261 unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
262 const unsigned char conf_rsp[] = { 0x04, 0x7b };
Johan Hedberg10122d02012-07-16 16:12:14 +0300263 const unsigned char wakeup_req[] = { 0x05, 0xfa };
264 const unsigned char woken_req[] = { 0x06, 0xf9 };
265 const unsigned char sleep_req[] = { 0x07, 0x78 };
Johan Hedberg40f10222012-07-16 16:12:09 +0300266 const unsigned char *hdr = h5->rx_skb->data;
267 const unsigned char *data = &h5->rx_skb->data[4];
268
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300269 BT_DBG("%s", hu->hdev->name);
Johan Hedberg40f10222012-07-16 16:12:09 +0300270
271 if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT)
272 return;
273
274 if (H5_HDR_LEN(hdr) < 2)
275 return;
276
Johan Hedbergafdc9442012-07-16 16:12:18 +0300277 conf_req[2] = h5_cfg_field(h5);
278
Johan Hedberg40f10222012-07-16 16:12:09 +0300279 if (memcmp(data, sync_req, 2) == 0) {
280 h5_link_control(hu, sync_rsp, 2);
281 } else if (memcmp(data, sync_rsp, 2) == 0) {
Johan Hedbergf674a052012-07-16 16:12:15 +0300282 h5->state = H5_INITIALIZED;
Johan Hedberg40f10222012-07-16 16:12:09 +0300283 h5_link_control(hu, conf_req, 3);
284 } else if (memcmp(data, conf_req, 2) == 0) {
285 h5_link_control(hu, conf_rsp, 2);
286 h5_link_control(hu, conf_req, 3);
287 } else if (memcmp(data, conf_rsp, 2) == 0) {
Johan Hedbergafdc9442012-07-16 16:12:18 +0300288 if (H5_HDR_LEN(hdr) > 2)
289 h5->tx_win = (data[2] & 7);
290 BT_DBG("Three-wire init complete. tx_win %u", h5->tx_win);
Johan Hedbergf674a052012-07-16 16:12:15 +0300291 h5->state = H5_ACTIVE;
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300292 hci_uart_init_ready(hu);
Johan Hedberg40f10222012-07-16 16:12:09 +0300293 return;
Johan Hedberg10122d02012-07-16 16:12:14 +0300294 } else if (memcmp(data, sleep_req, 2) == 0) {
295 BT_DBG("Peer went to sleep");
Johan Hedberg95c5c222012-07-16 16:12:16 +0300296 h5->sleep = H5_SLEEPING;
297 return;
Johan Hedberg10122d02012-07-16 16:12:14 +0300298 } else if (memcmp(data, woken_req, 2) == 0) {
299 BT_DBG("Peer woke up");
Johan Hedberg95c5c222012-07-16 16:12:16 +0300300 h5->sleep = H5_AWAKE;
301 } else if (memcmp(data, wakeup_req, 2) == 0) {
302 BT_DBG("Peer requested wakeup");
303 h5_link_control(hu, woken_req, 2);
304 h5->sleep = H5_AWAKE;
Johan Hedberg40f10222012-07-16 16:12:09 +0300305 } else {
306 BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
307 return;
308 }
309
310 hci_uart_tx_wakeup(hu);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300311}
312
313static void h5_complete_rx_pkt(struct hci_uart *hu)
314{
315 struct h5 *h5 = hu->priv;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300316 const unsigned char *hdr = h5->rx_skb->data;
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300317
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300318 if (H5_HDR_RELIABLE(hdr)) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300319 h5->tx_ack = (h5->tx_ack + 1) % 8;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300320 h5->tx_ack_req = true;
Johan Hedberg40f10222012-07-16 16:12:09 +0300321 hci_uart_tx_wakeup(hu);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300322 }
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300323
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300324 h5->rx_ack = H5_HDR_ACK(hdr);
325
326 h5_pkt_cull(h5);
327
328 switch (H5_HDR_PKT_TYPE(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300329 case HCI_EVENT_PKT:
330 case HCI_ACLDATA_PKT:
331 case HCI_SCODATA_PKT:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300332 bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300333
334 /* Remove Three-wire header */
335 skb_pull(h5->rx_skb, 4);
336
337 hci_recv_frame(h5->rx_skb);
338 h5->rx_skb = NULL;
339
340 break;
341
342 default:
343 h5_handle_internal_rx(hu);
344 break;
345 }
346
347 h5_reset_rx(h5);
348}
349
350static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
351{
352 struct h5 *h5 = hu->priv;
353
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300354 h5_complete_rx_pkt(hu);
355 h5_reset_rx(h5);
356
357 return 0;
358}
359
360static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
361{
362 struct h5 *h5 = hu->priv;
363 const unsigned char *hdr = h5->rx_skb->data;
364
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300365 if (H5_HDR_CRC(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300366 h5->rx_func = h5_rx_crc;
367 h5->rx_pending = 2;
368 } else {
369 h5_complete_rx_pkt(hu);
370 h5_reset_rx(h5);
371 }
372
373 return 0;
374}
375
376static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
377{
378 struct h5 *h5 = hu->priv;
379 const unsigned char *hdr = h5->rx_skb->data;
380
Johan Hedberg40f10222012-07-16 16:12:09 +0300381 BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u",
382 hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
383 H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
384 H5_HDR_LEN(hdr));
385
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300386 if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
387 BT_ERR("Invalid header checksum");
388 h5_reset_rx(h5);
389 return 0;
390 }
391
Johan Hedberg40f10222012-07-16 16:12:09 +0300392 if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300393 BT_ERR("Out-of-order packet arrived (%u != %u)",
Johan Hedberg40f10222012-07-16 16:12:09 +0300394 H5_HDR_SEQ(hdr), h5->tx_ack);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300395 h5_reset_rx(h5);
396 return 0;
397 }
398
Johan Hedbergf674a052012-07-16 16:12:15 +0300399 if (h5->state != H5_ACTIVE &&
400 H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) {
401 BT_ERR("Non-link packet received in non-active state");
402 h5_reset_rx(h5);
403 }
404
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300405 h5->rx_func = h5_rx_payload;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300406 h5->rx_pending = H5_HDR_LEN(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300407
408 return 0;
409}
410
411static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
412{
413 struct h5 *h5 = hu->priv;
414
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300415 if (c == SLIP_DELIMITER)
416 return 1;
417
418 h5->rx_func = h5_rx_3wire_hdr;
419 h5->rx_pending = 4;
420
421 h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
422 if (!h5->rx_skb) {
423 BT_ERR("Can't allocate mem for new packet");
424 h5_reset_rx(h5);
425 return -ENOMEM;
426 }
427
428 h5->rx_skb->dev = (void *) hu->hdev;
429
430 return 0;
431}
432
433static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
434{
435 struct h5 *h5 = hu->priv;
436
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300437 if (c == SLIP_DELIMITER)
438 h5->rx_func = h5_rx_pkt_start;
439
440 return 1;
441}
442
443static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
444{
445 const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
446 const u8 *byte = &c;
447
448 if (!h5->rx_esc && c == SLIP_ESC) {
449 h5->rx_esc = true;
450 return;
451 }
452
453 if (h5->rx_esc) {
454 switch (c) {
455 case SLIP_ESC_DELIM:
456 byte = &delim;
457 break;
458 case SLIP_ESC_ESC:
459 byte = &esc;
460 break;
461 default:
462 BT_ERR("Invalid esc byte 0x%02hhx", c);
463 h5_reset_rx(h5);
464 return;
465 }
466
467 h5->rx_esc = false;
468 }
469
470 memcpy(skb_put(h5->rx_skb, 1), byte, 1);
471 h5->rx_pending--;
472
Johan Hedberg255a68e2012-07-16 16:12:13 +0300473 BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300474}
475
476static void h5_reset_rx(struct h5 *h5)
477{
478 if (h5->rx_skb) {
479 kfree_skb(h5->rx_skb);
480 h5->rx_skb = NULL;
481 }
482
483 h5->rx_func = h5_rx_delimiter;
484 h5->rx_pending = 0;
485 h5->rx_esc = false;
486}
487
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300488static int h5_recv(struct hci_uart *hu, void *data, int count)
489{
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300490 struct h5 *h5 = hu->priv;
491 unsigned char *ptr = data;
492
Johan Hedberg255a68e2012-07-16 16:12:13 +0300493 BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
494 count);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300495
496 while (count > 0) {
497 int processed;
498
499 if (h5->rx_pending > 0) {
500 if (*ptr == SLIP_DELIMITER) {
501 BT_ERR("Too short H5 packet");
502 h5_reset_rx(h5);
503 continue;
504 }
505
506 h5_unslip_one_byte(h5, *ptr);
507
508 ptr++; count--;
509 continue;
510 }
511
512 processed = h5->rx_func(hu, *ptr);
513 if (processed < 0)
514 return processed;
515
516 ptr += processed;
517 count -= processed;
518 }
519
520 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300521}
522
523static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
524{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300525 struct h5 *h5 = hu->priv;
526
527 if (skb->len > 0xfff) {
528 BT_ERR("Packet too long (%u bytes)", skb->len);
529 kfree_skb(skb);
530 return 0;
531 }
532
Johan Hedbergf674a052012-07-16 16:12:15 +0300533 if (h5->state != H5_ACTIVE) {
534 BT_ERR("Ignoring HCI data in non-active state");
535 kfree_skb(skb);
536 return 0;
537 }
538
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300539 switch (bt_cb(skb)->pkt_type) {
540 case HCI_ACLDATA_PKT:
541 case HCI_COMMAND_PKT:
542 skb_queue_tail(&h5->rel, skb);
543 break;
544
545 case HCI_SCODATA_PKT:
546 skb_queue_tail(&h5->unrel, skb);
547 break;
548
549 default:
550 BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
551 kfree_skb(skb);
552 break;
553 }
554
555 return 0;
556}
557
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300558static void h5_slip_delim(struct sk_buff *skb)
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300559{
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300560 const char delim = SLIP_DELIMITER;
561
562 memcpy(skb_put(skb, 1), &delim, 1);
563}
564
565static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
566{
567 const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
568 const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
569
570 switch (c) {
571 case SLIP_DELIMITER:
572 memcpy(skb_put(skb, 2), &esc_delim, 2);
573 break;
574 case SLIP_ESC:
575 memcpy(skb_put(skb, 2), &esc_esc, 2);
576 break;
577 default:
578 memcpy(skb_put(skb, 1), &c, 1);
579 }
580}
581
Johan Hedbergc826ed02012-07-16 16:12:17 +0300582static bool valid_packet_type(u8 type)
583{
584 switch (type) {
585 case HCI_ACLDATA_PKT:
586 case HCI_COMMAND_PKT:
587 case HCI_SCODATA_PKT:
588 case HCI_3WIRE_LINK_PKT:
589 case HCI_3WIRE_ACK_PKT:
590 return true;
591 default:
592 return false;
593 }
594}
595
596static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
597 const u8 *data, size_t len)
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300598{
Johan Hedberg40f10222012-07-16 16:12:09 +0300599 struct h5 *h5 = hu->priv;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300600 struct sk_buff *nskb;
601 u8 hdr[4];
602 int i;
603
Johan Hedbergc826ed02012-07-16 16:12:17 +0300604 if (!valid_packet_type(pkt_type)) {
605 BT_ERR("Unknown packet type %u", pkt_type);
606 return NULL;
607 }
608
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300609 /*
610 * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
611 * (because bytes 0xc0 and 0xdb are escaped, worst case is when
612 * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
613 * delimiters at start and end).
614 */
615 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
616 if (!nskb)
617 return NULL;
618
619 bt_cb(nskb)->pkt_type = pkt_type;
620
621 h5_slip_delim(nskb);
622
Johan Hedberg40f10222012-07-16 16:12:09 +0300623 hdr[0] = h5->tx_ack << 3;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300624 h5->tx_ack_req = false;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300625
Johan Hedbergc826ed02012-07-16 16:12:17 +0300626 /* Reliable packet? */
627 if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300628 hdr[0] |= 1 << 7;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300629 hdr[0] |= h5->tx_seq;
630 h5->tx_seq = (h5->tx_seq + 1) % 8;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300631 }
632
633 hdr[1] = pkt_type | ((len & 0x0f) << 4);
634 hdr[2] = len >> 4;
635 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
636
Johan Hedberg40f10222012-07-16 16:12:09 +0300637 BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
638 hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
639 H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
640 H5_HDR_LEN(hdr));
641
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300642 for (i = 0; i < 4; i++)
643 h5_slip_one_byte(nskb, hdr[i]);
644
645 for (i = 0; i < len; i++)
646 h5_slip_one_byte(nskb, data[i]);
647
648 h5_slip_delim(nskb);
649
650 return nskb;
651}
652
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300653static struct sk_buff *h5_dequeue(struct hci_uart *hu)
654{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300655 struct h5 *h5 = hu->priv;
Johan Hedberg3f27e952012-07-16 16:12:04 +0300656 unsigned long flags;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300657 struct sk_buff *skb, *nskb;
658
Johan Hedberg95c5c222012-07-16 16:12:16 +0300659 if (h5->sleep != H5_AWAKE) {
660 const unsigned char wakeup_req[] = { 0x05, 0xfa };
661
662 if (h5->sleep == H5_WAKING_UP)
663 return NULL;
664
665 h5->sleep = H5_WAKING_UP;
666 BT_DBG("Sending wakeup request");
667
668 mod_timer(&h5->timer, jiffies + HZ / 100);
669 return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2);
670 }
671
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300672 if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300673 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300674 skb->data, skb->len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300675 if (nskb) {
676 kfree_skb(skb);
677 return nskb;
678 }
679
680 skb_queue_head(&h5->unrel, skb);
681 BT_ERR("Could not dequeue pkt because alloc_skb failed");
682 }
683
Johan Hedberg3f27e952012-07-16 16:12:04 +0300684 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
685
Johan Hedbergafdc9442012-07-16 16:12:18 +0300686 if (h5->unack.qlen >= h5->tx_win)
Johan Hedberg3f27e952012-07-16 16:12:04 +0300687 goto unlock;
688
689 if ((skb = skb_dequeue(&h5->rel)) != NULL) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300690 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300691 skb->data, skb->len);
Johan Hedberg3f27e952012-07-16 16:12:04 +0300692 if (nskb) {
693 __skb_queue_tail(&h5->unack, skb);
694 mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
695 spin_unlock_irqrestore(&h5->unack.lock, flags);
696 return nskb;
697 }
698
699 skb_queue_head(&h5->rel, skb);
700 BT_ERR("Could not dequeue pkt because alloc_skb failed");
701 }
702
703unlock:
704 spin_unlock_irqrestore(&h5->unack.lock, flags);
705
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300706 if (h5->tx_ack_req)
Johan Hedberg40f10222012-07-16 16:12:09 +0300707 return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300708
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300709 return NULL;
710}
711
712static int h5_flush(struct hci_uart *hu)
713{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300714 BT_DBG("hu %p", hu);
715 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300716}
717
718static struct hci_uart_proto h5p = {
719 .id = HCI_UART_3WIRE,
720 .open = h5_open,
721 .close = h5_close,
722 .recv = h5_recv,
723 .enqueue = h5_enqueue,
724 .dequeue = h5_dequeue,
725 .flush = h5_flush,
726};
727
728int __init h5_init(void)
729{
730 int err = hci_uart_register_proto(&h5p);
731
732 if (!err)
733 BT_INFO("HCI Three-wire UART (H5) protocol initialized");
734 else
735 BT_ERR("HCI Three-wire UART (H5) protocol init failed");
736
737 return err;
738}
739
740int __exit h5_deinit(void)
741{
742 return hci_uart_unregister_proto(&h5p);
743}