blob: 6fb8d4eca0fb5f2cdef85046e2ad22c0bb4ac4ac [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 Hedberg3f27e952012-07-16 16:12:04 +030036#define H5_TXWINSIZE 4
37
38#define H5_ACK_TIMEOUT msecs_to_jiffies(250)
Johan Hedberg40f10222012-07-16 16:12:09 +030039#define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
Johan Hedberg3f27e952012-07-16 16:12:04 +030040
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030041/*
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
Johan Hedberg01977c02012-07-16 16:12:07 +030047/* 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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030055#define SLIP_DELIMITER 0xc0
56#define SLIP_ESC 0xdb
57#define SLIP_ESC_DELIM 0xdc
58#define SLIP_ESC_ESC 0xdd
59
Johan Hedberg7d664fb2012-07-16 16:12:03 +030060struct h5 {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030061 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 */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030064
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030065 struct sk_buff *rx_skb; /* Receive buffer */
66 size_t rx_pending; /* Expecting more bytes */
67 bool rx_esc; /* SLIP escape mode */
Johan Hedberg43eb12d2012-07-16 16:12:08 +030068 u8 rx_ack; /* Last ack number received */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030069
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030070 int (*rx_func) (struct hci_uart *hu, u8 c);
Johan Hedberg3f27e952012-07-16 16:12:04 +030071
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030072 struct timer_list timer; /* Retransmission timer */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030073
Johan Hedberg43eb12d2012-07-16 16:12:08 +030074 bool tx_ack_req; /* Pending ack to send */
75 u8 tx_seq; /* Next seq number to send */
Johan Hedberg40f10222012-07-16 16:12:09 +030076 u8 tx_ack; /* Next ack number to send */
Johan Hedberg10122d02012-07-16 16:12:14 +030077
Johan Hedbergf674a052012-07-16 16:12:15 +030078 enum {
79 H5_UNINITIALIZED,
80 H5_INITIALIZED,
81 H5_ACTIVE,
82 } state;
83
Johan Hedberg10122d02012-07-16 16:12:14 +030084 bool sleeping;
Johan Hedberg7d664fb2012-07-16 16:12:03 +030085};
86
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030087static void h5_reset_rx(struct h5 *h5);
88
Johan Hedberg40f10222012-07-16 16:12:09 +030089static 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
Johan Hedbergf674a052012-07-16 16:12:15 +0300105static void h5_timed_event(unsigned long arg)
106{
107 const unsigned char sync_req[] = { 0x01, 0x7e };
108 const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
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
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
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) {
130 h5->tx_seq = (h5->tx_seq - 1) & 0x07;
131 skb_queue_head(&h5->rel, skb);
132 }
133
134 spin_unlock_irqrestore(&h5->unack.lock, flags);
135
136wakeup:
137 hci_uart_tx_wakeup(hu);
138}
139
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300140static int h5_open(struct hci_uart *hu)
141{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300142 struct h5 *h5;
Johan Hedberg40f10222012-07-16 16:12:09 +0300143 const unsigned char sync[] = { 0x01, 0x7e };
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300144
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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300157 h5_reset_rx(h5);
158
Johan Hedberg3f27e952012-07-16 16:12:04 +0300159 init_timer(&h5->timer);
160 h5->timer.function = h5_timed_event;
161 h5->timer.data = (unsigned long) hu;
162
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300163 set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
164
Johan Hedberg40f10222012-07-16 16:12:09 +0300165 /* Send initial sync request */
166 h5_link_control(hu, sync, sizeof(sync));
167 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
168
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300169 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300170}
171
172static int h5_close(struct hci_uart *hu)
173{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300174 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
Johan Hedberg3f27e952012-07-16 16:12:04 +0300180 del_timer(&h5->timer);
181
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300182 kfree(h5);
183
184 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300185}
186
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300187static 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);
Johan Hedberg40f10222012-07-16 16:12:09 +0300197 if (to_remove == 0)
198 goto unlock;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300199
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
Johan Hedberg40f10222012-07-16 16:12:09 +0300225unlock:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300226 spin_unlock_irqrestore(&h5->unack.lock, flags);
227}
228
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300229static void h5_handle_internal_rx(struct hci_uart *hu)
230{
Johan Hedberg40f10222012-07-16 16:12:09 +0300231 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 };
Johan Hedberg10122d02012-07-16 16:12:14 +0300236 const unsigned char wakeup_req[] = { 0x05, 0xfa };
237 const unsigned char woken_req[] = { 0x06, 0xf9 };
238 const unsigned char sleep_req[] = { 0x07, 0x78 };
Johan Hedberg40f10222012-07-16 16:12:09 +0300239 const unsigned char *hdr = h5->rx_skb->data;
240 const unsigned char *data = &h5->rx_skb->data[4];
241
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300242 BT_DBG("%s", hu->hdev->name);
Johan Hedberg40f10222012-07-16 16:12:09 +0300243
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) {
Johan Hedbergf674a052012-07-16 16:12:15 +0300253 h5->state = H5_INITIALIZED;
Johan Hedberg40f10222012-07-16 16:12:09 +0300254 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");
Johan Hedbergf674a052012-07-16 16:12:15 +0300260 h5->state = H5_ACTIVE;
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300261 hci_uart_init_ready(hu);
Johan Hedberg40f10222012-07-16 16:12:09 +0300262 return;
Johan Hedberg10122d02012-07-16 16:12:14 +0300263 } 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;
Johan Hedberg40f10222012-07-16 16:12:09 +0300271 } else {
272 BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
273 return;
274 }
275
276 hci_uart_tx_wakeup(hu);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300277}
278
279static void h5_complete_rx_pkt(struct hci_uart *hu)
280{
281 struct h5 *h5 = hu->priv;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300282 const unsigned char *hdr = h5->rx_skb->data;
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300283
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300284 if (H5_HDR_RELIABLE(hdr)) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300285 h5->tx_ack = (h5->tx_ack + 1) % 8;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300286 h5->tx_ack_req = true;
Johan Hedberg40f10222012-07-16 16:12:09 +0300287 hci_uart_tx_wakeup(hu);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300288 }
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300289
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300290 h5->rx_ack = H5_HDR_ACK(hdr);
291
292 h5_pkt_cull(h5);
293
294 switch (H5_HDR_PKT_TYPE(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300295 case HCI_EVENT_PKT:
296 case HCI_ACLDATA_PKT:
297 case HCI_SCODATA_PKT:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300298 bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300299
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
316static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
317{
318 struct h5 *h5 = hu->priv;
319
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300320 h5_complete_rx_pkt(hu);
321 h5_reset_rx(h5);
322
323 return 0;
324}
325
326static 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
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300331 if (H5_HDR_CRC(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300332 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
342static 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300347 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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300352 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300358 if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300359 BT_ERR("Out-of-order packet arrived (%u != %u)",
Johan Hedberg40f10222012-07-16 16:12:09 +0300360 H5_HDR_SEQ(hdr), h5->tx_ack);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300361 h5_reset_rx(h5);
362 return 0;
363 }
364
Johan Hedbergf674a052012-07-16 16:12:15 +0300365 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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300371 h5->rx_func = h5_rx_payload;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300372 h5->rx_pending = H5_HDR_LEN(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300373
374 return 0;
375}
376
377static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
378{
379 struct h5 *h5 = hu->priv;
380
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300381 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
399static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
400{
401 struct h5 *h5 = hu->priv;
402
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300403 if (c == SLIP_DELIMITER)
404 h5->rx_func = h5_rx_pkt_start;
405
406 return 1;
407}
408
409static 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
Johan Hedberg255a68e2012-07-16 16:12:13 +0300439 BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300440}
441
442static 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
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300454static int h5_recv(struct hci_uart *hu, void *data, int count)
455{
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300456 struct h5 *h5 = hu->priv;
457 unsigned char *ptr = data;
458
Johan Hedberg255a68e2012-07-16 16:12:13 +0300459 BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
460 count);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300461
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;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300487}
488
489static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
490{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300491 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
Johan Hedbergf674a052012-07-16 16:12:15 +0300499 if (h5->state != H5_ACTIVE) {
500 BT_ERR("Ignoring HCI data in non-active state");
501 kfree_skb(skb);
502 return 0;
503 }
504
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300505 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
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300524static void h5_slip_delim(struct sk_buff *skb)
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300525{
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300526 const char delim = SLIP_DELIMITER;
527
528 memcpy(skb_put(skb, 1), &delim, 1);
529}
530
531static 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300548static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300549 const u8 *data, size_t len)
550{
Johan Hedberg40f10222012-07-16 16:12:09 +0300551 struct h5 *h5 = hu->priv;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300552 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300570 hdr[0] = h5->tx_ack << 3;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300571 h5->tx_ack_req = false;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300572
573 if (rel) {
574 hdr[0] |= 1 << 7;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300575 hdr[0] |= h5->tx_seq;
576 h5->tx_seq = (h5->tx_seq + 1) % 8;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300577 }
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
Johan Hedberg40f10222012-07-16 16:12:09 +0300583 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
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300588 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300599static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300600 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300619 return h5_build_pkt(hu, rel, pkt_type, data, len);
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300620}
621
622static struct sk_buff *h5_dequeue(struct hci_uart *hu)
623{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300624 struct h5 *h5 = hu->priv;
Johan Hedberg3f27e952012-07-16 16:12:04 +0300625 unsigned long flags;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300626 struct sk_buff *skb, *nskb;
627
628 if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300629 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300630 skb->data, skb->len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300631 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
Johan Hedberg3f27e952012-07-16 16:12:04 +0300640 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) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300646 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300647 skb->data, skb->len);
Johan Hedberg3f27e952012-07-16 16:12:04 +0300648 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
659unlock:
660 spin_unlock_irqrestore(&h5->unack.lock, flags);
661
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300662 if (h5->tx_ack_req)
Johan Hedberg40f10222012-07-16 16:12:09 +0300663 return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300664
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300665 return NULL;
666}
667
668static int h5_flush(struct hci_uart *hu)
669{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300670 BT_DBG("hu %p", hu);
671 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300672}
673
674static 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
684int __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
696int __exit h5_deinit(void)
697{
698 return hci_uart_unregister_proto(&h5p);
699}