blob: f9067ce25568c3a0721173ccb9144e0ed52d2532 [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 Hedberg95c5c222012-07-16 16:12:16 +030084 enum {
85 H5_AWAKE,
86 H5_SLEEPING,
87 H5_WAKING_UP,
88 } sleep;
Johan Hedberg7d664fb2012-07-16 16:12:03 +030089};
90
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030091static void h5_reset_rx(struct h5 *h5);
92
Johan Hedberg40f10222012-07-16 16:12:09 +030093static 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
Johan Hedbergf674a052012-07-16 16:12:15 +0300109static void h5_timed_event(unsigned long arg)
110{
111 const unsigned char sync_req[] = { 0x01, 0x7e };
112 const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
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
Johan Hedberg95c5c222012-07-16 16:12:16 +0300118 BT_DBG("%s", hu->hdev->name);
119
Johan Hedbergf674a052012-07-16 16:12:15 +0300120 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
Johan Hedberg95c5c222012-07-16 16:12:16 +0300131 if (h5->sleep != H5_AWAKE) {
132 h5->sleep = H5_SLEEPING;
133 goto wakeup;
134 }
135
Johan Hedbergf674a052012-07-16 16:12:15 +0300136 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) {
141 h5->tx_seq = (h5->tx_seq - 1) & 0x07;
142 skb_queue_head(&h5->rel, skb);
143 }
144
145 spin_unlock_irqrestore(&h5->unack.lock, flags);
146
147wakeup:
148 hci_uart_tx_wakeup(hu);
149}
150
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300151static int h5_open(struct hci_uart *hu)
152{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300153 struct h5 *h5;
Johan Hedberg40f10222012-07-16 16:12:09 +0300154 const unsigned char sync[] = { 0x01, 0x7e };
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300155
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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300168 h5_reset_rx(h5);
169
Johan Hedberg3f27e952012-07-16 16:12:04 +0300170 init_timer(&h5->timer);
171 h5->timer.function = h5_timed_event;
172 h5->timer.data = (unsigned long) hu;
173
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300174 set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
175
Johan Hedberg40f10222012-07-16 16:12:09 +0300176 /* Send initial sync request */
177 h5_link_control(hu, sync, sizeof(sync));
178 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
179
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300180 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300181}
182
183static int h5_close(struct hci_uart *hu)
184{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300185 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
Johan Hedberg3f27e952012-07-16 16:12:04 +0300191 del_timer(&h5->timer);
192
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300193 kfree(h5);
194
195 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300196}
197
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300198static 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);
Johan Hedberg40f10222012-07-16 16:12:09 +0300208 if (to_remove == 0)
209 goto unlock;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300210
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
Johan Hedberg40f10222012-07-16 16:12:09 +0300236unlock:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300237 spin_unlock_irqrestore(&h5->unack.lock, flags);
238}
239
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300240static void h5_handle_internal_rx(struct hci_uart *hu)
241{
Johan Hedberg40f10222012-07-16 16:12:09 +0300242 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 };
Johan Hedberg10122d02012-07-16 16:12:14 +0300247 const unsigned char wakeup_req[] = { 0x05, 0xfa };
248 const unsigned char woken_req[] = { 0x06, 0xf9 };
249 const unsigned char sleep_req[] = { 0x07, 0x78 };
Johan Hedberg40f10222012-07-16 16:12:09 +0300250 const unsigned char *hdr = h5->rx_skb->data;
251 const unsigned char *data = &h5->rx_skb->data[4];
252
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300253 BT_DBG("%s", hu->hdev->name);
Johan Hedberg40f10222012-07-16 16:12:09 +0300254
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) {
Johan Hedbergf674a052012-07-16 16:12:15 +0300264 h5->state = H5_INITIALIZED;
Johan Hedberg40f10222012-07-16 16:12:09 +0300265 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");
Johan Hedbergf674a052012-07-16 16:12:15 +0300271 h5->state = H5_ACTIVE;
Johan Hedbergcd1b4422012-07-16 16:12:12 +0300272 hci_uart_init_ready(hu);
Johan Hedberg40f10222012-07-16 16:12:09 +0300273 return;
Johan Hedberg10122d02012-07-16 16:12:14 +0300274 } else if (memcmp(data, sleep_req, 2) == 0) {
275 BT_DBG("Peer went to sleep");
Johan Hedberg95c5c222012-07-16 16:12:16 +0300276 h5->sleep = H5_SLEEPING;
277 return;
Johan Hedberg10122d02012-07-16 16:12:14 +0300278 } else if (memcmp(data, woken_req, 2) == 0) {
279 BT_DBG("Peer woke up");
Johan Hedberg95c5c222012-07-16 16:12:16 +0300280 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;
Johan Hedberg40f10222012-07-16 16:12:09 +0300285 } else {
286 BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
287 return;
288 }
289
290 hci_uart_tx_wakeup(hu);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300291}
292
293static void h5_complete_rx_pkt(struct hci_uart *hu)
294{
295 struct h5 *h5 = hu->priv;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300296 const unsigned char *hdr = h5->rx_skb->data;
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300297
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300298 if (H5_HDR_RELIABLE(hdr)) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300299 h5->tx_ack = (h5->tx_ack + 1) % 8;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300300 h5->tx_ack_req = true;
Johan Hedberg40f10222012-07-16 16:12:09 +0300301 hci_uart_tx_wakeup(hu);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300302 }
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300303
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300304 h5->rx_ack = H5_HDR_ACK(hdr);
305
306 h5_pkt_cull(h5);
307
308 switch (H5_HDR_PKT_TYPE(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300309 case HCI_EVENT_PKT:
310 case HCI_ACLDATA_PKT:
311 case HCI_SCODATA_PKT:
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300312 bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300313
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
330static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
331{
332 struct h5 *h5 = hu->priv;
333
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300334 h5_complete_rx_pkt(hu);
335 h5_reset_rx(h5);
336
337 return 0;
338}
339
340static 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
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300345 if (H5_HDR_CRC(hdr)) {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300346 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
356static 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300361 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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300366 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
Johan Hedberg40f10222012-07-16 16:12:09 +0300372 if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300373 BT_ERR("Out-of-order packet arrived (%u != %u)",
Johan Hedberg40f10222012-07-16 16:12:09 +0300374 H5_HDR_SEQ(hdr), h5->tx_ack);
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300375 h5_reset_rx(h5);
376 return 0;
377 }
378
Johan Hedbergf674a052012-07-16 16:12:15 +0300379 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
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300385 h5->rx_func = h5_rx_payload;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300386 h5->rx_pending = H5_HDR_LEN(hdr);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300387
388 return 0;
389}
390
391static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
392{
393 struct h5 *h5 = hu->priv;
394
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300395 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
413static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
414{
415 struct h5 *h5 = hu->priv;
416
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300417 if (c == SLIP_DELIMITER)
418 h5->rx_func = h5_rx_pkt_start;
419
420 return 1;
421}
422
423static 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
Johan Hedberg255a68e2012-07-16 16:12:13 +0300453 BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300454}
455
456static 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
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300468static int h5_recv(struct hci_uart *hu, void *data, int count)
469{
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300470 struct h5 *h5 = hu->priv;
471 unsigned char *ptr = data;
472
Johan Hedberg255a68e2012-07-16 16:12:13 +0300473 BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
474 count);
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300475
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;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300501}
502
503static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
504{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300505 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
Johan Hedbergf674a052012-07-16 16:12:15 +0300513 if (h5->state != H5_ACTIVE) {
514 BT_ERR("Ignoring HCI data in non-active state");
515 kfree_skb(skb);
516 return 0;
517 }
518
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300519 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
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300538static void h5_slip_delim(struct sk_buff *skb)
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300539{
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300540 const char delim = SLIP_DELIMITER;
541
542 memcpy(skb_put(skb, 1), &delim, 1);
543}
544
545static 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
Johan Hedbergc826ed02012-07-16 16:12:17 +0300562static bool valid_packet_type(u8 type)
563{
564 switch (type) {
565 case HCI_ACLDATA_PKT:
566 case HCI_COMMAND_PKT:
567 case HCI_SCODATA_PKT:
568 case HCI_3WIRE_LINK_PKT:
569 case HCI_3WIRE_ACK_PKT:
570 return true;
571 default:
572 return false;
573 }
574}
575
576static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
577 const u8 *data, size_t len)
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300578{
Johan Hedberg40f10222012-07-16 16:12:09 +0300579 struct h5 *h5 = hu->priv;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300580 struct sk_buff *nskb;
581 u8 hdr[4];
582 int i;
583
Johan Hedbergc826ed02012-07-16 16:12:17 +0300584 if (!valid_packet_type(pkt_type)) {
585 BT_ERR("Unknown packet type %u", pkt_type);
586 return NULL;
587 }
588
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300589 /*
590 * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
591 * (because bytes 0xc0 and 0xdb are escaped, worst case is when
592 * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
593 * delimiters at start and end).
594 */
595 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
596 if (!nskb)
597 return NULL;
598
599 bt_cb(nskb)->pkt_type = pkt_type;
600
601 h5_slip_delim(nskb);
602
Johan Hedberg40f10222012-07-16 16:12:09 +0300603 hdr[0] = h5->tx_ack << 3;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300604 h5->tx_ack_req = false;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300605
Johan Hedbergc826ed02012-07-16 16:12:17 +0300606 /* Reliable packet? */
607 if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300608 hdr[0] |= 1 << 7;
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300609 hdr[0] |= h5->tx_seq;
610 h5->tx_seq = (h5->tx_seq + 1) % 8;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300611 }
612
613 hdr[1] = pkt_type | ((len & 0x0f) << 4);
614 hdr[2] = len >> 4;
615 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
616
Johan Hedberg40f10222012-07-16 16:12:09 +0300617 BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
618 hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
619 H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
620 H5_HDR_LEN(hdr));
621
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300622 for (i = 0; i < 4; i++)
623 h5_slip_one_byte(nskb, hdr[i]);
624
625 for (i = 0; i < len; i++)
626 h5_slip_one_byte(nskb, data[i]);
627
628 h5_slip_delim(nskb);
629
630 return nskb;
631}
632
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300633static struct sk_buff *h5_dequeue(struct hci_uart *hu)
634{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300635 struct h5 *h5 = hu->priv;
Johan Hedberg3f27e952012-07-16 16:12:04 +0300636 unsigned long flags;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300637 struct sk_buff *skb, *nskb;
638
Johan Hedberg95c5c222012-07-16 16:12:16 +0300639 if (h5->sleep != H5_AWAKE) {
640 const unsigned char wakeup_req[] = { 0x05, 0xfa };
641
642 if (h5->sleep == H5_WAKING_UP)
643 return NULL;
644
645 h5->sleep = H5_WAKING_UP;
646 BT_DBG("Sending wakeup request");
647
648 mod_timer(&h5->timer, jiffies + HZ / 100);
649 return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2);
650 }
651
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300652 if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300653 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300654 skb->data, skb->len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300655 if (nskb) {
656 kfree_skb(skb);
657 return nskb;
658 }
659
660 skb_queue_head(&h5->unrel, skb);
661 BT_ERR("Could not dequeue pkt because alloc_skb failed");
662 }
663
Johan Hedberg3f27e952012-07-16 16:12:04 +0300664 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
665
666 if (h5->unack.qlen >= H5_TXWINSIZE)
667 goto unlock;
668
669 if ((skb = skb_dequeue(&h5->rel)) != NULL) {
Johan Hedberg40f10222012-07-16 16:12:09 +0300670 nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300671 skb->data, skb->len);
Johan Hedberg3f27e952012-07-16 16:12:04 +0300672 if (nskb) {
673 __skb_queue_tail(&h5->unack, skb);
674 mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
675 spin_unlock_irqrestore(&h5->unack.lock, flags);
676 return nskb;
677 }
678
679 skb_queue_head(&h5->rel, skb);
680 BT_ERR("Could not dequeue pkt because alloc_skb failed");
681 }
682
683unlock:
684 spin_unlock_irqrestore(&h5->unack.lock, flags);
685
Johan Hedberg43eb12d2012-07-16 16:12:08 +0300686 if (h5->tx_ack_req)
Johan Hedberg40f10222012-07-16 16:12:09 +0300687 return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300688
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300689 return NULL;
690}
691
692static int h5_flush(struct hci_uart *hu)
693{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300694 BT_DBG("hu %p", hu);
695 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300696}
697
698static struct hci_uart_proto h5p = {
699 .id = HCI_UART_3WIRE,
700 .open = h5_open,
701 .close = h5_close,
702 .recv = h5_recv,
703 .enqueue = h5_enqueue,
704 .dequeue = h5_dequeue,
705 .flush = h5_flush,
706};
707
708int __init h5_init(void)
709{
710 int err = hci_uart_register_proto(&h5p);
711
712 if (!err)
713 BT_INFO("HCI Three-wire UART (H5) protocol initialized");
714 else
715 BT_ERR("HCI Three-wire UART (H5) protocol init failed");
716
717 return err;
718}
719
720int __exit h5_deinit(void)
721{
722 return hci_uart_unregister_proto(&h5p);
723}