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 | ||
95c5c220 JH |
84 | enum { |
85 | H5_AWAKE, | |
86 | H5_SLEEPING, | |
87 | H5_WAKING_UP, | |
88 | } sleep; | |
7d664fba JH |
89 | }; |
90 | ||
bc1f35b9 JH |
91 | static void h5_reset_rx(struct h5 *h5); |
92 | ||
f674a057 JH |
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 | ||
3f27e95b JH |
109 | static void h5_timed_event(unsigned long arg) |
110 | { | |
f674a057 JH |
111 | const unsigned char sync_req[] = { 0x01, 0x7e }; |
112 | const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 }; | |
3f27e95b JH |
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 | ||
95c5c220 JH |
118 | BT_DBG("%s", hu->hdev->name); |
119 | ||
f674a057 JH |
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 | ||
95c5c220 JH |
131 | if (h5->sleep != H5_AWAKE) { |
132 | h5->sleep = H5_SLEEPING; | |
133 | goto wakeup; | |
134 | } | |
135 | ||
3f27e95b JH |
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) { | |
43eb12d7 | 141 | h5->tx_seq = (h5->tx_seq - 1) & 0x07; |
3f27e95b JH |
142 | skb_queue_head(&h5->rel, skb); |
143 | } | |
144 | ||
145 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
146 | ||
f674a057 | 147 | wakeup: |
3f27e95b JH |
148 | hci_uart_tx_wakeup(hu); |
149 | } | |
150 | ||
7dec65c8 JH |
151 | static int h5_open(struct hci_uart *hu) |
152 | { | |
7d664fba | 153 | struct h5 *h5; |
40f10224 | 154 | const unsigned char sync[] = { 0x01, 0x7e }; |
7d664fba JH |
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 | ||
bc1f35b9 JH |
168 | h5_reset_rx(h5); |
169 | ||
3f27e95b JH |
170 | init_timer(&h5->timer); |
171 | h5->timer.function = h5_timed_event; | |
172 | h5->timer.data = (unsigned long) hu; | |
173 | ||
cd1b4425 JH |
174 | set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags); |
175 | ||
40f10224 JH |
176 | /* Send initial sync request */ |
177 | h5_link_control(hu, sync, sizeof(sync)); | |
178 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); | |
179 | ||
7d664fba | 180 | return 0; |
7dec65c8 JH |
181 | } |
182 | ||
183 | static int h5_close(struct hci_uart *hu) | |
184 | { | |
7d664fba JH |
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 | ||
3f27e95b JH |
191 | del_timer(&h5->timer); |
192 | ||
7d664fba JH |
193 | kfree(h5); |
194 | ||
195 | return 0; | |
7dec65c8 JH |
196 | } |
197 | ||
43eb12d7 JH |
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); | |
40f10224 JH |
208 | if (to_remove == 0) |
209 | goto unlock; | |
43eb12d7 JH |
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 | ||
40f10224 | 236 | unlock: |
43eb12d7 JH |
237 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
238 | } | |
239 | ||
bc1f35b9 JH |
240 | static void h5_handle_internal_rx(struct hci_uart *hu) |
241 | { | |
40f10224 JH |
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 }; | |
10122d07 JH |
247 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; |
248 | const unsigned char woken_req[] = { 0x06, 0xf9 }; | |
249 | const unsigned char sleep_req[] = { 0x07, 0x78 }; | |
40f10224 JH |
250 | const unsigned char *hdr = h5->rx_skb->data; |
251 | const unsigned char *data = &h5->rx_skb->data[4]; | |
252 | ||
bc1f35b9 | 253 | BT_DBG("%s", hu->hdev->name); |
40f10224 JH |
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) { | |
f674a057 | 264 | h5->state = H5_INITIALIZED; |
40f10224 JH |
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"); | |
f674a057 | 271 | h5->state = H5_ACTIVE; |
cd1b4425 | 272 | hci_uart_init_ready(hu); |
40f10224 | 273 | return; |
10122d07 JH |
274 | } else if (memcmp(data, sleep_req, 2) == 0) { |
275 | BT_DBG("Peer went to sleep"); | |
95c5c220 JH |
276 | h5->sleep = H5_SLEEPING; |
277 | return; | |
10122d07 JH |
278 | } else if (memcmp(data, woken_req, 2) == 0) { |
279 | BT_DBG("Peer woke up"); | |
95c5c220 JH |
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; | |
40f10224 JH |
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); | |
bc1f35b9 JH |
291 | } |
292 | ||
293 | static void h5_complete_rx_pkt(struct hci_uart *hu) | |
294 | { | |
295 | struct h5 *h5 = hu->priv; | |
43eb12d7 | 296 | const unsigned char *hdr = h5->rx_skb->data; |
bc1f35b9 | 297 | |
43eb12d7 | 298 | if (H5_HDR_RELIABLE(hdr)) { |
40f10224 | 299 | h5->tx_ack = (h5->tx_ack + 1) % 8; |
43eb12d7 | 300 | h5->tx_ack_req = true; |
40f10224 | 301 | hci_uart_tx_wakeup(hu); |
43eb12d7 | 302 | } |
bc1f35b9 | 303 | |
43eb12d7 JH |
304 | h5->rx_ack = H5_HDR_ACK(hdr); |
305 | ||
306 | h5_pkt_cull(h5); | |
307 | ||
308 | switch (H5_HDR_PKT_TYPE(hdr)) { | |
bc1f35b9 JH |
309 | case HCI_EVENT_PKT: |
310 | case HCI_ACLDATA_PKT: | |
311 | case HCI_SCODATA_PKT: | |
43eb12d7 | 312 | bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr); |
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
43eb12d7 | 345 | if (H5_HDR_CRC(hdr)) { |
bc1f35b9 JH |
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 | ||
40f10224 JH |
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 | ||
bc1f35b9 JH |
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 | ||
40f10224 | 372 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) { |
43eb12d7 | 373 | BT_ERR("Out-of-order packet arrived (%u != %u)", |
40f10224 | 374 | H5_HDR_SEQ(hdr), h5->tx_ack); |
43eb12d7 JH |
375 | h5_reset_rx(h5); |
376 | return 0; | |
377 | } | |
378 | ||
f674a057 JH |
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 | ||
bc1f35b9 | 385 | h5->rx_func = h5_rx_payload; |
43eb12d7 | 386 | h5->rx_pending = H5_HDR_LEN(hdr); |
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
bc1f35b9 JH |
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 | ||
255a68e0 | 453 | BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending); |
bc1f35b9 JH |
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 | ||
7dec65c8 JH |
468 | static int h5_recv(struct hci_uart *hu, void *data, int count) |
469 | { | |
bc1f35b9 JH |
470 | struct h5 *h5 = hu->priv; |
471 | unsigned char *ptr = data; | |
472 | ||
255a68e0 JH |
473 | BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending, |
474 | count); | |
bc1f35b9 JH |
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; | |
7dec65c8 JH |
501 | } |
502 | ||
503 | static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
504 | { | |
7d664fba JH |
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 | ||
f674a057 JH |
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 | ||
7d664fba JH |
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 | ||
c0a1b73c JH |
538 | static void h5_slip_delim(struct sk_buff *skb) |
539 | { | |
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 | ||
40f10224 | 562 | static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type, |
c0a1b73c | 563 | const u8 *data, size_t len) |
7d664fba | 564 | { |
40f10224 | 565 | struct h5 *h5 = hu->priv; |
c0a1b73c JH |
566 | struct sk_buff *nskb; |
567 | u8 hdr[4]; | |
568 | int i; | |
569 | ||
570 | /* | |
571 | * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2 | |
572 | * (because bytes 0xc0 and 0xdb are escaped, worst case is when | |
573 | * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0 | |
574 | * delimiters at start and end). | |
575 | */ | |
576 | nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC); | |
577 | if (!nskb) | |
578 | return NULL; | |
579 | ||
580 | bt_cb(nskb)->pkt_type = pkt_type; | |
581 | ||
582 | h5_slip_delim(nskb); | |
583 | ||
40f10224 | 584 | hdr[0] = h5->tx_ack << 3; |
43eb12d7 | 585 | h5->tx_ack_req = false; |
c0a1b73c JH |
586 | |
587 | if (rel) { | |
588 | hdr[0] |= 1 << 7; | |
43eb12d7 JH |
589 | hdr[0] |= h5->tx_seq; |
590 | h5->tx_seq = (h5->tx_seq + 1) % 8; | |
c0a1b73c JH |
591 | } |
592 | ||
593 | hdr[1] = pkt_type | ((len & 0x0f) << 4); | |
594 | hdr[2] = len >> 4; | |
595 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); | |
596 | ||
40f10224 JH |
597 | BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u", |
598 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), | |
599 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), | |
600 | H5_HDR_LEN(hdr)); | |
601 | ||
c0a1b73c JH |
602 | for (i = 0; i < 4; i++) |
603 | h5_slip_one_byte(nskb, hdr[i]); | |
604 | ||
605 | for (i = 0; i < len; i++) | |
606 | h5_slip_one_byte(nskb, data[i]); | |
607 | ||
608 | h5_slip_delim(nskb); | |
609 | ||
610 | return nskb; | |
611 | } | |
612 | ||
40f10224 | 613 | static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type, |
c0a1b73c JH |
614 | const u8 *data, size_t len) |
615 | { | |
616 | bool rel; | |
617 | ||
618 | switch (pkt_type) { | |
619 | case HCI_ACLDATA_PKT: | |
620 | case HCI_COMMAND_PKT: | |
621 | rel = true; | |
622 | break; | |
623 | case HCI_SCODATA_PKT: | |
624 | case HCI_3WIRE_LINK_PKT: | |
625 | case HCI_3WIRE_ACK_PKT: | |
626 | rel = false; | |
627 | break; | |
628 | default: | |
629 | BT_ERR("Unknown packet type %u", pkt_type); | |
630 | return NULL; | |
631 | } | |
632 | ||
40f10224 | 633 | return h5_build_pkt(hu, rel, pkt_type, data, len); |
7dec65c8 JH |
634 | } |
635 | ||
636 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) | |
637 | { | |
7d664fba | 638 | struct h5 *h5 = hu->priv; |
3f27e95b | 639 | unsigned long flags; |
7d664fba JH |
640 | struct sk_buff *skb, *nskb; |
641 | ||
95c5c220 JH |
642 | if (h5->sleep != H5_AWAKE) { |
643 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; | |
644 | ||
645 | if (h5->sleep == H5_WAKING_UP) | |
646 | return NULL; | |
647 | ||
648 | h5->sleep = H5_WAKING_UP; | |
649 | BT_DBG("Sending wakeup request"); | |
650 | ||
651 | mod_timer(&h5->timer, jiffies + HZ / 100); | |
652 | return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2); | |
653 | } | |
654 | ||
7d664fba | 655 | if ((skb = skb_dequeue(&h5->unrel)) != NULL) { |
40f10224 | 656 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
c0a1b73c | 657 | skb->data, skb->len); |
7d664fba JH |
658 | if (nskb) { |
659 | kfree_skb(skb); | |
660 | return nskb; | |
661 | } | |
662 | ||
663 | skb_queue_head(&h5->unrel, skb); | |
664 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); | |
665 | } | |
666 | ||
3f27e95b JH |
667 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
668 | ||
669 | if (h5->unack.qlen >= H5_TXWINSIZE) | |
670 | goto unlock; | |
671 | ||
672 | if ((skb = skb_dequeue(&h5->rel)) != NULL) { | |
40f10224 | 673 | nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type, |
c0a1b73c | 674 | skb->data, skb->len); |
3f27e95b JH |
675 | if (nskb) { |
676 | __skb_queue_tail(&h5->unack, skb); | |
677 | mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT); | |
678 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
679 | return nskb; | |
680 | } | |
681 | ||
682 | skb_queue_head(&h5->rel, skb); | |
683 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); | |
684 | } | |
685 | ||
686 | unlock: | |
687 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
688 | ||
43eb12d7 | 689 | if (h5->tx_ack_req) |
40f10224 | 690 | return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0); |
7d664fba | 691 | |
7dec65c8 JH |
692 | return NULL; |
693 | } | |
694 | ||
695 | static int h5_flush(struct hci_uart *hu) | |
696 | { | |
7d664fba JH |
697 | BT_DBG("hu %p", hu); |
698 | return 0; | |
7dec65c8 JH |
699 | } |
700 | ||
701 | static struct hci_uart_proto h5p = { | |
702 | .id = HCI_UART_3WIRE, | |
703 | .open = h5_open, | |
704 | .close = h5_close, | |
705 | .recv = h5_recv, | |
706 | .enqueue = h5_enqueue, | |
707 | .dequeue = h5_dequeue, | |
708 | .flush = h5_flush, | |
709 | }; | |
710 | ||
711 | int __init h5_init(void) | |
712 | { | |
713 | int err = hci_uart_register_proto(&h5p); | |
714 | ||
715 | if (!err) | |
716 | BT_INFO("HCI Three-wire UART (H5) protocol initialized"); | |
717 | else | |
718 | BT_ERR("HCI Three-wire UART (H5) protocol init failed"); | |
719 | ||
720 | return err; | |
721 | } | |
722 | ||
723 | int __exit h5_deinit(void) | |
724 | { | |
725 | return hci_uart_unregister_proto(&h5p); | |
726 | } |