Commit | Line | Data |
---|---|---|
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 | 60 | struct 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 */ |
10122d07 | 77 | |
f674a057 JH |
78 | enum { |
79 | H5_UNINITIALIZED, | |
80 | H5_INITIALIZED, | |
81 | H5_ACTIVE, | |
82 | } state; | |
83 | ||
10122d07 | 84 | bool sleeping; |
7d664fba JH |
85 | }; |
86 | ||
bc1f35b9 JH |
87 | static void h5_reset_rx(struct h5 *h5); |
88 | ||
f674a057 JH |
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 | ||
3f27e95b JH |
105 | static void h5_timed_event(unsigned long arg) |
106 | { | |
f674a057 JH |
107 | const unsigned char sync_req[] = { 0x01, 0x7e }; |
108 | const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 }; | |
3f27e95b JH |
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 | ||
f674a057 JH |
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 | ||
3f27e95b JH |
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) { | |
43eb12d7 | 130 | h5->tx_seq = (h5->tx_seq - 1) & 0x07; |
3f27e95b JH |
131 | skb_queue_head(&h5->rel, skb); |
132 | } | |
133 | ||
134 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
135 | ||
f674a057 | 136 | wakeup: |
3f27e95b JH |
137 | hci_uart_tx_wakeup(hu); |
138 | } | |
139 | ||
7dec65c8 JH |
140 | static int h5_open(struct hci_uart *hu) |
141 | { | |
7d664fba | 142 | struct h5 *h5; |
40f10224 | 143 | const unsigned char sync[] = { 0x01, 0x7e }; |
7d664fba JH |
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 | ||
bc1f35b9 JH |
157 | h5_reset_rx(h5); |
158 | ||
3f27e95b JH |
159 | init_timer(&h5->timer); |
160 | h5->timer.function = h5_timed_event; | |
161 | h5->timer.data = (unsigned long) hu; | |
162 | ||
cd1b4425 JH |
163 | set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags); |
164 | ||
40f10224 JH |
165 | /* Send initial sync request */ |
166 | h5_link_control(hu, sync, sizeof(sync)); | |
167 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); | |
168 | ||
7d664fba | 169 | return 0; |
7dec65c8 JH |
170 | } |
171 | ||
172 | static int h5_close(struct hci_uart *hu) | |
173 | { | |
7d664fba JH |
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 | ||
3f27e95b JH |
180 | del_timer(&h5->timer); |
181 | ||
7d664fba JH |
182 | kfree(h5); |
183 | ||
184 | return 0; | |
7dec65c8 JH |
185 | } |
186 | ||
43eb12d7 JH |
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); | |
40f10224 JH |
197 | if (to_remove == 0) |
198 | goto unlock; | |
43eb12d7 JH |
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 | ||
40f10224 | 225 | unlock: |
43eb12d7 JH |
226 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
227 | } | |
228 | ||
bc1f35b9 JH |
229 | static void h5_handle_internal_rx(struct hci_uart *hu) |
230 | { | |
40f10224 JH |
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 }; | |
10122d07 JH |
236 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; |
237 | const unsigned char woken_req[] = { 0x06, 0xf9 }; | |
238 | const unsigned char sleep_req[] = { 0x07, 0x78 }; | |
40f10224 JH |
239 | const unsigned char *hdr = h5->rx_skb->data; |
240 | const unsigned char *data = &h5->rx_skb->data[4]; | |
241 | ||
bc1f35b9 | 242 | BT_DBG("%s", hu->hdev->name); |
40f10224 JH |
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) { | |
f674a057 | 253 | h5->state = H5_INITIALIZED; |
40f10224 JH |
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"); | |
f674a057 | 260 | h5->state = H5_ACTIVE; |
cd1b4425 | 261 | hci_uart_init_ready(hu); |
40f10224 | 262 | return; |
10122d07 JH |
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; | |
40f10224 JH |
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); | |
bc1f35b9 JH |
277 | } |
278 | ||
279 | static void h5_complete_rx_pkt(struct hci_uart *hu) | |
280 | { | |
281 | struct h5 *h5 = hu->priv; | |
43eb12d7 | 282 | const unsigned char *hdr = h5->rx_skb->data; |
bc1f35b9 | 283 | |
43eb12d7 | 284 | if (H5_HDR_RELIABLE(hdr)) { |
40f10224 | 285 | h5->tx_ack = (h5->tx_ack + 1) % 8; |
43eb12d7 | 286 | h5->tx_ack_req = true; |
40f10224 | 287 | hci_uart_tx_wakeup(hu); |
43eb12d7 | 288 | } |
bc1f35b9 | 289 | |
43eb12d7 JH |
290 | h5->rx_ack = H5_HDR_ACK(hdr); |
291 | ||
292 | h5_pkt_cull(h5); | |
293 | ||
294 | switch (H5_HDR_PKT_TYPE(hdr)) { | |
bc1f35b9 JH |
295 | case HCI_EVENT_PKT: |
296 | case HCI_ACLDATA_PKT: | |
297 | case HCI_SCODATA_PKT: | |
43eb12d7 | 298 | bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr); |
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
43eb12d7 | 331 | if (H5_HDR_CRC(hdr)) { |
bc1f35b9 JH |
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 | ||
40f10224 JH |
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 | ||
bc1f35b9 JH |
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 | ||
40f10224 | 358 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) { |
43eb12d7 | 359 | BT_ERR("Out-of-order packet arrived (%u != %u)", |
40f10224 | 360 | H5_HDR_SEQ(hdr), h5->tx_ack); |
43eb12d7 JH |
361 | h5_reset_rx(h5); |
362 | return 0; | |
363 | } | |
364 | ||
f674a057 JH |
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 | ||
bc1f35b9 | 371 | h5->rx_func = h5_rx_payload; |
43eb12d7 | 372 | h5->rx_pending = H5_HDR_LEN(hdr); |
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
255a68e0 | 439 | BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending); |
bc1f35b9 JH |
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 | ||
7dec65c8 JH |
454 | static int h5_recv(struct hci_uart *hu, void *data, int count) |
455 | { | |
bc1f35b9 JH |
456 | struct h5 *h5 = hu->priv; |
457 | unsigned char *ptr = data; | |
458 | ||
255a68e0 JH |
459 | BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending, |
460 | count); | |
bc1f35b9 JH |
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; | |
7dec65c8 JH |
487 | } |
488 | ||
489 | static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
490 | { | |
7d664fba JH |
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 | ||
f674a057 JH |
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 | ||
7d664fba JH |
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 | ||
c0a1b73c JH |
524 | static void h5_slip_delim(struct sk_buff *skb) |
525 | { | |
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 | ||
40f10224 | 548 | static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type, |
c0a1b73c | 549 | const u8 *data, size_t len) |
7d664fba | 550 | { |
40f10224 | 551 | struct h5 *h5 = hu->priv; |
c0a1b73c JH |
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 | ||
40f10224 | 570 | hdr[0] = h5->tx_ack << 3; |
43eb12d7 | 571 | h5->tx_ack_req = false; |
c0a1b73c JH |
572 | |
573 | if (rel) { | |
574 | hdr[0] |= 1 << 7; | |
43eb12d7 JH |
575 | hdr[0] |= h5->tx_seq; |
576 | h5->tx_seq = (h5->tx_seq + 1) % 8; | |
c0a1b73c JH |
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 | ||
40f10224 JH |
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 | ||
c0a1b73c JH |
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 | ||
40f10224 | 599 | static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type, |
c0a1b73c JH |
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 | ||
40f10224 | 619 | return h5_build_pkt(hu, rel, pkt_type, data, len); |
7dec65c8 JH |
620 | } |
621 | ||
622 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) | |
623 | { | |
7d664fba | 624 | struct h5 *h5 = hu->priv; |
3f27e95b | 625 | unsigned long flags; |
7d664fba JH |
626 | struct sk_buff *skb, *nskb; |
627 | ||
628 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { | |
40f10224 | 629 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
c0a1b73c | 630 | skb->data, skb->len); |
7d664fba JH |
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 | ||
3f27e95b JH |
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) { | |
40f10224 | 646 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
c0a1b73c | 647 | skb->data, skb->len); |
3f27e95b JH |
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 | ||
43eb12d7 | 662 | if (h5->tx_ack_req) |
40f10224 | 663 | return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0); |
7d664fba | 664 | |
7dec65c8 JH |
665 | return NULL; |
666 | } | |
667 | ||
668 | static int h5_flush(struct hci_uart *hu) | |
669 | { | |
7d664fba JH |
670 | BT_DBG("hu %p", hu); |
671 | return 0; | |
7dec65c8 JH |
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 | } |