3 * Bluetooth HCI Three-wire UART driver
5 * Copyright (C) 2012 Intel Corporation
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.
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.
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
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/skbuff.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
33 #define HCI_3WIRE_ACK_PKT 0
34 #define HCI_3WIRE_LINK_PKT 15
36 #define H5_TXWINSIZE 4
38 #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
41 * Maximum Three-wire packet:
42 * 4 byte header + max value for 12-bit length + 2 bytes for CRC
44 #define H5_MAX_LEN (4 + 0xfff + 2)
46 #define SLIP_DELIMITER 0xc0
48 #define SLIP_ESC_DELIM 0xdc
49 #define SLIP_ESC_ESC 0xdd
52 struct sk_buff_head unack
; /* Unack'ed packets queue */
53 struct sk_buff_head rel
; /* Reliable packets queue */
54 struct sk_buff_head unrel
; /* Unreliable packets queue */
56 struct sk_buff
*rx_skb
; /* Receive buffer */
57 size_t rx_pending
; /* Expecting more bytes */
58 bool rx_esc
; /* SLIP escape mode */
60 int (*rx_func
) (struct hci_uart
*hu
, u8 c
);
62 struct timer_list timer
; /* Retransmission timer */
70 static void h5_reset_rx(struct h5
*h5
);
72 static void h5_timed_event(unsigned long arg
)
74 struct hci_uart
*hu
= (struct hci_uart
*) arg
;
75 struct h5
*h5
= hu
->priv
;
79 BT_DBG("hu %p retransmitting %u pkts", hu
, h5
->unack
.qlen
);
81 spin_lock_irqsave_nested(&h5
->unack
.lock
, flags
, SINGLE_DEPTH_NESTING
);
83 while ((skb
= __skb_dequeue_tail(&h5
->unack
)) != NULL
) {
84 h5
->next_seq
= (h5
->next_seq
- 1) & 0x07;
85 skb_queue_head(&h5
->rel
, skb
);
88 spin_unlock_irqrestore(&h5
->unack
.lock
, flags
);
90 hci_uart_tx_wakeup(hu
);
93 static int h5_open(struct hci_uart
*hu
)
99 h5
= kzalloc(sizeof(*h5
), GFP_KERNEL
);
105 skb_queue_head_init(&h5
->unack
);
106 skb_queue_head_init(&h5
->rel
);
107 skb_queue_head_init(&h5
->unrel
);
111 init_timer(&h5
->timer
);
112 h5
->timer
.function
= h5_timed_event
;
113 h5
->timer
.data
= (unsigned long) hu
;
118 static int h5_close(struct hci_uart
*hu
)
120 struct h5
*h5
= hu
->priv
;
122 skb_queue_purge(&h5
->unack
);
123 skb_queue_purge(&h5
->rel
);
124 skb_queue_purge(&h5
->unrel
);
126 del_timer(&h5
->timer
);
133 static void h5_handle_internal_rx(struct hci_uart
*hu
)
135 BT_DBG("%s", hu
->hdev
->name
);
138 static void h5_complete_rx_pkt(struct hci_uart
*hu
)
140 struct h5
*h5
= hu
->priv
;
143 BT_DBG("%s", hu
->hdev
->name
);
145 pkt_type
= h5
->rx_skb
->data
[1] & 0x0f;
149 case HCI_ACLDATA_PKT
:
150 case HCI_SCODATA_PKT
:
151 bt_cb(h5
->rx_skb
)->pkt_type
= pkt_type
;
153 /* Remove Three-wire header */
154 skb_pull(h5
->rx_skb
, 4);
156 hci_recv_frame(h5
->rx_skb
);
162 h5_handle_internal_rx(hu
);
169 static int h5_rx_crc(struct hci_uart
*hu
, unsigned char c
)
171 struct h5
*h5
= hu
->priv
;
173 BT_DBG("%s 0x%02hhx", hu
->hdev
->name
, c
);
175 h5_complete_rx_pkt(hu
);
181 static int h5_rx_payload(struct hci_uart
*hu
, unsigned char c
)
183 struct h5
*h5
= hu
->priv
;
184 const unsigned char *hdr
= h5
->rx_skb
->data
;
186 BT_DBG("%s 0x%02hhx", hu
->hdev
->name
, c
);
188 if ((hdr
[0] >> 4) & 0x01) {
189 h5
->rx_func
= h5_rx_crc
;
192 h5_complete_rx_pkt(hu
);
199 static int h5_rx_3wire_hdr(struct hci_uart
*hu
, unsigned char c
)
201 struct h5
*h5
= hu
->priv
;
202 const unsigned char *hdr
= h5
->rx_skb
->data
;
204 BT_DBG("%s 0x%02hhx", hu
->hdev
->name
, c
);
206 if (((hdr
[0] + hdr
[1] + hdr
[2] + hdr
[3]) & 0xff) != 0xff) {
207 BT_ERR("Invalid header checksum");
212 h5
->rx_func
= h5_rx_payload
;
213 h5
->rx_pending
= ((hdr
[1] >> 4) & 0xff) + (hdr
[2] << 4);
218 static int h5_rx_pkt_start(struct hci_uart
*hu
, unsigned char c
)
220 struct h5
*h5
= hu
->priv
;
222 BT_DBG("%s 0x%02hhx", hu
->hdev
->name
, c
);
224 if (c
== SLIP_DELIMITER
)
227 h5
->rx_func
= h5_rx_3wire_hdr
;
230 h5
->rx_skb
= bt_skb_alloc(H5_MAX_LEN
, GFP_ATOMIC
);
232 BT_ERR("Can't allocate mem for new packet");
237 h5
->rx_skb
->dev
= (void *) hu
->hdev
;
242 static int h5_rx_delimiter(struct hci_uart
*hu
, unsigned char c
)
244 struct h5
*h5
= hu
->priv
;
246 BT_DBG("%s 0x%02hhx", hu
->hdev
->name
, c
);
248 if (c
== SLIP_DELIMITER
)
249 h5
->rx_func
= h5_rx_pkt_start
;
254 static void h5_unslip_one_byte(struct h5
*h5
, unsigned char c
)
256 const u8 delim
= SLIP_DELIMITER
, esc
= SLIP_ESC
;
259 if (!h5
->rx_esc
&& c
== SLIP_ESC
) {
273 BT_ERR("Invalid esc byte 0x%02hhx", c
);
281 memcpy(skb_put(h5
->rx_skb
, 1), byte
, 1);
284 BT_DBG("unsliped 0x%02hhx", *byte
);
287 static void h5_reset_rx(struct h5
*h5
)
290 kfree_skb(h5
->rx_skb
);
294 h5
->rx_func
= h5_rx_delimiter
;
299 static int h5_recv(struct hci_uart
*hu
, void *data
, int count
)
301 struct h5
*h5
= hu
->priv
;
302 unsigned char *ptr
= data
;
304 BT_DBG("%s count %d", hu
->hdev
->name
, count
);
309 if (h5
->rx_pending
> 0) {
310 if (*ptr
== SLIP_DELIMITER
) {
311 BT_ERR("Too short H5 packet");
316 h5_unslip_one_byte(h5
, *ptr
);
322 processed
= h5
->rx_func(hu
, *ptr
);
333 static int h5_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
335 struct h5
*h5
= hu
->priv
;
337 if (skb
->len
> 0xfff) {
338 BT_ERR("Packet too long (%u bytes)", skb
->len
);
343 switch (bt_cb(skb
)->pkt_type
) {
344 case HCI_ACLDATA_PKT
:
345 case HCI_COMMAND_PKT
:
346 skb_queue_tail(&h5
->rel
, skb
);
349 case HCI_SCODATA_PKT
:
350 skb_queue_tail(&h5
->unrel
, skb
);
354 BT_ERR("Unknown packet type %u", bt_cb(skb
)->pkt_type
);
362 static void h5_slip_delim(struct sk_buff
*skb
)
364 const char delim
= SLIP_DELIMITER
;
366 memcpy(skb_put(skb
, 1), &delim
, 1);
369 static void h5_slip_one_byte(struct sk_buff
*skb
, u8 c
)
371 const char esc_delim
[2] = { SLIP_ESC
, SLIP_ESC_DELIM
};
372 const char esc_esc
[2] = { SLIP_ESC
, SLIP_ESC_ESC
};
376 memcpy(skb_put(skb
, 2), &esc_delim
, 2);
379 memcpy(skb_put(skb
, 2), &esc_esc
, 2);
382 memcpy(skb_put(skb
, 1), &c
, 1);
386 static struct sk_buff
*h5_build_pkt(struct h5
*h5
, bool rel
, u8 pkt_type
,
387 const u8
*data
, size_t len
)
389 struct sk_buff
*nskb
;
394 * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
395 * (because bytes 0xc0 and 0xdb are escaped, worst case is when
396 * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
397 * delimiters at start and end).
399 nskb
= alloc_skb((len
+ 6) * 2 + 2, GFP_ATOMIC
);
403 bt_cb(nskb
)->pkt_type
= pkt_type
;
407 hdr
[0] = h5
->next_ack
<< 3;
408 h5
->txack_req
= false;
412 hdr
[0] |= h5
->next_seq
;
413 h5
->next_seq
= (h5
->next_seq
+ 1) % 8;
416 hdr
[1] = pkt_type
| ((len
& 0x0f) << 4);
418 hdr
[3] = ~((hdr
[0] + hdr
[1] + hdr
[2]) & 0xff);
420 for (i
= 0; i
< 4; i
++)
421 h5_slip_one_byte(nskb
, hdr
[i
]);
423 for (i
= 0; i
< len
; i
++)
424 h5_slip_one_byte(nskb
, data
[i
]);
431 static struct sk_buff
*h5_prepare_pkt(struct h5
*h5
, u8 pkt_type
,
432 const u8
*data
, size_t len
)
437 case HCI_ACLDATA_PKT
:
438 case HCI_COMMAND_PKT
:
441 case HCI_SCODATA_PKT
:
442 case HCI_3WIRE_LINK_PKT
:
443 case HCI_3WIRE_ACK_PKT
:
447 BT_ERR("Unknown packet type %u", pkt_type
);
451 return h5_build_pkt(h5
, rel
, pkt_type
, data
, len
);
454 static struct sk_buff
*h5_prepare_ack(struct h5
*h5
)
456 h5
->txack_req
= false;
460 static struct sk_buff
*h5_dequeue(struct hci_uart
*hu
)
462 struct h5
*h5
= hu
->priv
;
464 struct sk_buff
*skb
, *nskb
;
466 if ((skb
= skb_dequeue(&h5
->unrel
)) != NULL
) {
467 nskb
= h5_prepare_pkt(h5
, bt_cb(skb
)->pkt_type
,
468 skb
->data
, skb
->len
);
474 skb_queue_head(&h5
->unrel
, skb
);
475 BT_ERR("Could not dequeue pkt because alloc_skb failed");
478 spin_lock_irqsave_nested(&h5
->unack
.lock
, flags
, SINGLE_DEPTH_NESTING
);
480 if (h5
->unack
.qlen
>= H5_TXWINSIZE
)
483 if ((skb
= skb_dequeue(&h5
->rel
)) != NULL
) {
484 nskb
= h5_prepare_pkt(h5
, bt_cb(skb
)->pkt_type
,
485 skb
->data
, skb
->len
);
487 __skb_queue_tail(&h5
->unack
, skb
);
488 mod_timer(&h5
->timer
, jiffies
+ H5_ACK_TIMEOUT
);
489 spin_unlock_irqrestore(&h5
->unack
.lock
, flags
);
493 skb_queue_head(&h5
->rel
, skb
);
494 BT_ERR("Could not dequeue pkt because alloc_skb failed");
498 spin_unlock_irqrestore(&h5
->unack
.lock
, flags
);
501 return h5_prepare_ack(h5
);
506 static int h5_flush(struct hci_uart
*hu
)
512 static struct hci_uart_proto h5p
= {
513 .id
= HCI_UART_3WIRE
,
517 .enqueue
= h5_enqueue
,
518 .dequeue
= h5_dequeue
,
522 int __init
h5_init(void)
524 int err
= hci_uart_register_proto(&h5p
);
527 BT_INFO("HCI Three-wire UART (H5) protocol initialized");
529 BT_ERR("HCI Three-wire UART (H5) protocol init failed");
534 int __exit
h5_deinit(void)
536 return hci_uart_unregister_proto(&h5p
);
This page took 0.041013 seconds and 6 git commands to generate.