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