diff options
author | Johan Hedberg <johan.hedberg@intel.com> | 2012-07-16 09:12:09 -0400 |
---|---|---|
committer | Gustavo Padovan <gustavo.padovan@collabora.co.uk> | 2012-07-17 13:48:26 -0400 |
commit | 40f10224df6f00b59ab342e3d9d9ac8b50c8eada (patch) | |
tree | 448d140e03794c33f20b07c5350ece0d2d500e8b /drivers/bluetooth | |
parent | 43eb12d7896063d06011baab7097944a70c7e45a (diff) |
Bluetooth: Add support for Three-wire Link Control packets
This patch adds basic support for parsing and sending Three-wire UART
Link Control packets.
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Diffstat (limited to 'drivers/bluetooth')
-rw-r--r-- | drivers/bluetooth/hci_h5.c | 96 |
1 files changed, 79 insertions, 17 deletions
diff --git a/drivers/bluetooth/hci_h5.c b/drivers/bluetooth/hci_h5.c index 6c7b27e921a5..022a6bcb4323 100644 --- a/drivers/bluetooth/hci_h5.c +++ b/drivers/bluetooth/hci_h5.c | |||
@@ -36,6 +36,7 @@ | |||
36 | #define H5_TXWINSIZE 4 | 36 | #define H5_TXWINSIZE 4 |
37 | 37 | ||
38 | #define H5_ACK_TIMEOUT msecs_to_jiffies(250) | 38 | #define H5_ACK_TIMEOUT msecs_to_jiffies(250) |
39 | #define H5_SYNC_TIMEOUT msecs_to_jiffies(100) | ||
39 | 40 | ||
40 | /* | 41 | /* |
41 | * Maximum Three-wire packet: | 42 | * Maximum Three-wire packet: |
@@ -65,7 +66,6 @@ struct h5 { | |||
65 | size_t rx_pending; /* Expecting more bytes */ | 66 | size_t rx_pending; /* Expecting more bytes */ |
66 | bool rx_esc; /* SLIP escape mode */ | 67 | bool rx_esc; /* SLIP escape mode */ |
67 | u8 rx_ack; /* Last ack number received */ | 68 | u8 rx_ack; /* Last ack number received */ |
68 | u8 rx_seq; /* Last seq number received */ | ||
69 | 69 | ||
70 | int (*rx_func) (struct hci_uart *hu, u8 c); | 70 | int (*rx_func) (struct hci_uart *hu, u8 c); |
71 | 71 | ||
@@ -73,6 +73,7 @@ struct h5 { | |||
73 | 73 | ||
74 | bool tx_ack_req; /* Pending ack to send */ | 74 | bool tx_ack_req; /* Pending ack to send */ |
75 | u8 tx_seq; /* Next seq number to send */ | 75 | u8 tx_seq; /* Next seq number to send */ |
76 | u8 tx_ack; /* Next ack number to send */ | ||
76 | }; | 77 | }; |
77 | 78 | ||
78 | static void h5_reset_rx(struct h5 *h5); | 79 | static void h5_reset_rx(struct h5 *h5); |
@@ -98,9 +99,26 @@ static void h5_timed_event(unsigned long arg) | |||
98 | hci_uart_tx_wakeup(hu); | 99 | hci_uart_tx_wakeup(hu); |
99 | } | 100 | } |
100 | 101 | ||
102 | static 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 | |||
101 | static int h5_open(struct hci_uart *hu) | 118 | static int h5_open(struct hci_uart *hu) |
102 | { | 119 | { |
103 | struct h5 *h5; | 120 | struct h5 *h5; |
121 | const unsigned char sync[] = { 0x01, 0x7e }; | ||
104 | 122 | ||
105 | BT_DBG("hu %p", hu); | 123 | BT_DBG("hu %p", hu); |
106 | 124 | ||
@@ -120,6 +138,10 @@ static int h5_open(struct hci_uart *hu) | |||
120 | h5->timer.function = h5_timed_event; | 138 | h5->timer.function = h5_timed_event; |
121 | h5->timer.data = (unsigned long) hu; | 139 | h5->timer.data = (unsigned long) hu; |
122 | 140 | ||
141 | /* Send initial sync request */ | ||
142 | h5_link_control(hu, sync, sizeof(sync)); | ||
143 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); | ||
144 | |||
123 | return 0; | 145 | return 0; |
124 | } | 146 | } |
125 | 147 | ||
@@ -148,6 +170,8 @@ static void h5_pkt_cull(struct h5 *h5) | |||
148 | spin_lock_irqsave(&h5->unack.lock, flags); | 170 | spin_lock_irqsave(&h5->unack.lock, flags); |
149 | 171 | ||
150 | to_remove = skb_queue_len(&h5->unack); | 172 | to_remove = skb_queue_len(&h5->unack); |
173 | if (to_remove == 0) | ||
174 | goto unlock; | ||
151 | 175 | ||
152 | seq = h5->tx_seq; | 176 | seq = h5->tx_seq; |
153 | 177 | ||
@@ -174,12 +198,44 @@ static void h5_pkt_cull(struct h5 *h5) | |||
174 | if (skb_queue_empty(&h5->unack)) | 198 | if (skb_queue_empty(&h5->unack)) |
175 | del_timer(&h5->timer); | 199 | del_timer(&h5->timer); |
176 | 200 | ||
201 | unlock: | ||
177 | spin_unlock_irqrestore(&h5->unack.lock, flags); | 202 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
178 | } | 203 | } |
179 | 204 | ||
180 | static void h5_handle_internal_rx(struct hci_uart *hu) | 205 | static void h5_handle_internal_rx(struct hci_uart *hu) |
181 | { | 206 | { |
207 | struct h5 *h5 = hu->priv; | ||
208 | const unsigned char sync_req[] = { 0x01, 0x7e }; | ||
209 | const unsigned char sync_rsp[] = { 0x02, 0x7d }; | ||
210 | const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 }; | ||
211 | const unsigned char conf_rsp[] = { 0x04, 0x7b, 0x01 }; | ||
212 | const unsigned char *hdr = h5->rx_skb->data; | ||
213 | const unsigned char *data = &h5->rx_skb->data[4]; | ||
214 | |||
182 | BT_DBG("%s", hu->hdev->name); | 215 | BT_DBG("%s", hu->hdev->name); |
216 | |||
217 | if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) | ||
218 | return; | ||
219 | |||
220 | if (H5_HDR_LEN(hdr) < 2) | ||
221 | return; | ||
222 | |||
223 | if (memcmp(data, sync_req, 2) == 0) { | ||
224 | h5_link_control(hu, sync_rsp, 2); | ||
225 | } else if (memcmp(data, sync_rsp, 2) == 0) { | ||
226 | h5_link_control(hu, conf_req, 3); | ||
227 | } else if (memcmp(data, conf_req, 2) == 0) { | ||
228 | h5_link_control(hu, conf_rsp, 2); | ||
229 | h5_link_control(hu, conf_req, 3); | ||
230 | } else if (memcmp(data, conf_rsp, 2) == 0) { | ||
231 | BT_DBG("Three-wire init sequence complete"); | ||
232 | return; | ||
233 | } else { | ||
234 | BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]); | ||
235 | return; | ||
236 | } | ||
237 | |||
238 | hci_uart_tx_wakeup(hu); | ||
183 | } | 239 | } |
184 | 240 | ||
185 | static void h5_complete_rx_pkt(struct hci_uart *hu) | 241 | static void h5_complete_rx_pkt(struct hci_uart *hu) |
@@ -190,8 +246,9 @@ static void h5_complete_rx_pkt(struct hci_uart *hu) | |||
190 | BT_DBG("%s", hu->hdev->name); | 246 | BT_DBG("%s", hu->hdev->name); |
191 | 247 | ||
192 | if (H5_HDR_RELIABLE(hdr)) { | 248 | if (H5_HDR_RELIABLE(hdr)) { |
193 | h5->tx_seq = (h5->tx_seq + 1) % 8; | 249 | h5->tx_ack = (h5->tx_ack + 1) % 8; |
194 | h5->tx_ack_req = true; | 250 | h5->tx_ack_req = true; |
251 | hci_uart_tx_wakeup(hu); | ||
195 | } | 252 | } |
196 | 253 | ||
197 | h5->rx_ack = H5_HDR_ACK(hdr); | 254 | h5->rx_ack = H5_HDR_ACK(hdr); |
@@ -257,15 +314,20 @@ static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c) | |||
257 | 314 | ||
258 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); | 315 | BT_DBG("%s 0x%02hhx", hu->hdev->name, c); |
259 | 316 | ||
317 | BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u", | ||
318 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), | ||
319 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), | ||
320 | H5_HDR_LEN(hdr)); | ||
321 | |||
260 | if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) { | 322 | if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) { |
261 | BT_ERR("Invalid header checksum"); | 323 | BT_ERR("Invalid header checksum"); |
262 | h5_reset_rx(h5); | 324 | h5_reset_rx(h5); |
263 | return 0; | 325 | return 0; |
264 | } | 326 | } |
265 | 327 | ||
266 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_seq) { | 328 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) { |
267 | BT_ERR("Out-of-order packet arrived (%u != %u)", | 329 | BT_ERR("Out-of-order packet arrived (%u != %u)", |
268 | H5_HDR_SEQ(hdr), h5->tx_seq); | 330 | H5_HDR_SEQ(hdr), h5->tx_ack); |
269 | h5_reset_rx(h5); | 331 | h5_reset_rx(h5); |
270 | return 0; | 332 | return 0; |
271 | } | 333 | } |
@@ -444,9 +506,10 @@ static void h5_slip_one_byte(struct sk_buff *skb, u8 c) | |||
444 | } | 506 | } |
445 | } | 507 | } |
446 | 508 | ||
447 | static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type, | 509 | static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type, |
448 | const u8 *data, size_t len) | 510 | const u8 *data, size_t len) |
449 | { | 511 | { |
512 | struct h5 *h5 = hu->priv; | ||
450 | struct sk_buff *nskb; | 513 | struct sk_buff *nskb; |
451 | u8 hdr[4]; | 514 | u8 hdr[4]; |
452 | int i; | 515 | int i; |
@@ -465,7 +528,7 @@ static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type, | |||
465 | 528 | ||
466 | h5_slip_delim(nskb); | 529 | h5_slip_delim(nskb); |
467 | 530 | ||
468 | hdr[0] = h5->rx_seq << 3; | 531 | hdr[0] = h5->tx_ack << 3; |
469 | h5->tx_ack_req = false; | 532 | h5->tx_ack_req = false; |
470 | 533 | ||
471 | if (rel) { | 534 | if (rel) { |
@@ -478,6 +541,11 @@ static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type, | |||
478 | hdr[2] = len >> 4; | 541 | hdr[2] = len >> 4; |
479 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); | 542 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); |
480 | 543 | ||
544 | BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u", | ||
545 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), | ||
546 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), | ||
547 | H5_HDR_LEN(hdr)); | ||
548 | |||
481 | for (i = 0; i < 4; i++) | 549 | for (i = 0; i < 4; i++) |
482 | h5_slip_one_byte(nskb, hdr[i]); | 550 | h5_slip_one_byte(nskb, hdr[i]); |
483 | 551 | ||
@@ -489,7 +557,7 @@ static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type, | |||
489 | return nskb; | 557 | return nskb; |
490 | } | 558 | } |
491 | 559 | ||
492 | static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type, | 560 | static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type, |
493 | const u8 *data, size_t len) | 561 | const u8 *data, size_t len) |
494 | { | 562 | { |
495 | bool rel; | 563 | bool rel; |
@@ -509,13 +577,7 @@ static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type, | |||
509 | return NULL; | 577 | return NULL; |
510 | } | 578 | } |
511 | 579 | ||
512 | return h5_build_pkt(h5, rel, pkt_type, data, len); | 580 | return h5_build_pkt(hu, rel, pkt_type, data, len); |
513 | } | ||
514 | |||
515 | static struct sk_buff *h5_prepare_ack(struct h5 *h5) | ||
516 | { | ||
517 | h5->tx_ack_req = false; | ||
518 | return NULL; | ||
519 | } | 581 | } |
520 | 582 | ||
521 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) | 583 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) |
@@ -525,7 +587,7 @@ static struct sk_buff *h5_dequeue(struct hci_uart *hu) | |||
525 | struct sk_buff *skb, *nskb; | 587 | struct sk_buff *skb, *nskb; |
526 | 588 | ||
527 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { | 589 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { |
528 | nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type, | 590 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
529 | skb->data, skb->len); | 591 | skb->data, skb->len); |
530 | if (nskb) { | 592 | if (nskb) { |
531 | kfree_skb(skb); | 593 | kfree_skb(skb); |
@@ -542,7 +604,7 @@ static struct sk_buff *h5_dequeue(struct hci_uart *hu) | |||
542 | goto unlock; | 604 | goto unlock; |
543 | 605 | ||
544 | if ((skb = skb_dequeue(&h5->rel)) != NULL) { | 606 | if ((skb = skb_dequeue(&h5->rel)) != NULL) { |
545 | nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type, | 607 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
546 | skb->data, skb->len); | 608 | skb->data, skb->len); |
547 | if (nskb) { | 609 | if (nskb) { |
548 | __skb_queue_tail(&h5->unack, skb); | 610 | __skb_queue_tail(&h5->unack, skb); |
@@ -559,7 +621,7 @@ unlock: | |||
559 | spin_unlock_irqrestore(&h5->unack.lock, flags); | 621 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
560 | 622 | ||
561 | if (h5->tx_ack_req) | 623 | if (h5->tx_ack_req) |
562 | return h5_prepare_ack(h5); | 624 | return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0); |
563 | 625 | ||
564 | return NULL; | 626 | return NULL; |
565 | } | 627 | } |