24 #include <linux/kernel.h>
25 #include <linux/errno.h>
33 #define HCI_3WIRE_ACK_PKT 0
34 #define HCI_3WIRE_LINK_PKT 15
37 #define H5_TX_WIN_MAX 4
39 #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
40 #define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
46 #define H5_MAX_LEN (4 + 0xfff + 2)
49 #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
50 #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
51 #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
52 #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
53 #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
54 #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4))
56 #define SLIP_DELIMITER 0xc0
58 #define SLIP_ESC_DELIM 0xdc
59 #define SLIP_ESC_ESC 0xdd
99 static void h5_reset_rx(
struct h5 *
h5);
101 static void h5_link_control(
struct hci_uart *hu,
const void *
data,
size_t len)
117 static u8 h5_cfg_field(
struct h5 *h5)
122 field |= (h5->
tx_win & 7);
127 static void h5_timed_event(
unsigned long arg)
129 const unsigned char sync_req[] = { 0x01, 0x7e };
130 unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
132 struct h5 *h5 = hu->
priv;
138 if (h5->
state == H5_UNINITIALIZED)
139 h5_link_control(hu, sync_req,
sizeof(sync_req));
141 if (h5->
state == H5_INITIALIZED) {
142 conf_req[2] = h5_cfg_field(h5);
143 h5_link_control(hu, conf_req,
sizeof(conf_req));
146 if (h5->
state != H5_ACTIVE) {
151 if (h5->
sleep != H5_AWAKE) {
152 h5->
sleep = H5_SLEEPING;
156 BT_DBG(
"hu %p retransmitting %u pkts", hu, h5->
unack.qlen);
160 while ((skb = __skb_dequeue_tail(&h5->
unack)) !=
NULL) {
165 spin_unlock_irqrestore(&h5->
unack.lock, flags);
171 static int h5_open(
struct hci_uart *hu)
174 const unsigned char sync[] = { 0x01, 0x7e };
184 skb_queue_head_init(&h5->
unack);
185 skb_queue_head_init(&h5->
rel);
186 skb_queue_head_init(&h5->
unrel);
191 h5->
timer.function = h5_timed_event;
199 h5_link_control(hu, sync,
sizeof(sync));
205 static int h5_close(
struct hci_uart *hu)
207 struct h5 *h5 = hu->
priv;
220 static void h5_pkt_cull(
struct h5 *h5)
229 to_remove = skb_queue_len(&h5->
unack);
235 while (to_remove > 0) {
244 BT_ERR(
"Controller acked invalid packet");
247 skb_queue_walk_safe(&h5->
unack, skb, tmp) {
248 if (i++ >= to_remove)
251 __skb_unlink(skb, &h5->
unack);
255 if (skb_queue_empty(&h5->
unack))
259 spin_unlock_irqrestore(&h5->
unack.lock, flags);
262 static void h5_handle_internal_rx(
struct hci_uart *hu)
264 struct h5 *h5 = hu->
priv;
265 const unsigned char sync_req[] = { 0x01, 0x7e };
266 const unsigned char sync_rsp[] = { 0x02, 0x7d };
267 unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
268 const unsigned char conf_rsp[] = { 0x04, 0x7b };
269 const unsigned char wakeup_req[] = { 0x05, 0xfa };
270 const unsigned char woken_req[] = { 0x06, 0xf9 };
271 const unsigned char sleep_req[] = { 0x07, 0x78 };
272 const unsigned char *
hdr = h5->
rx_skb->data;
273 const unsigned char *data = &h5->
rx_skb->data[4];
283 conf_req[2] = h5_cfg_field(h5);
285 if (
memcmp(data, sync_req, 2) == 0) {
286 h5_link_control(hu, sync_rsp, 2);
287 }
else if (
memcmp(data, sync_rsp, 2) == 0) {
289 h5_link_control(hu, conf_req, 3);
290 }
else if (
memcmp(data, conf_req, 2) == 0) {
291 h5_link_control(hu, conf_rsp, 2);
292 h5_link_control(hu, conf_req, 3);
293 }
else if (
memcmp(data, conf_rsp, 2) == 0) {
295 h5->
tx_win = (data[2] & 7);
296 BT_DBG(
"Three-wire init complete. tx_win %u", h5->
tx_win);
300 }
else if (
memcmp(data, sleep_req, 2) == 0) {
301 BT_DBG(
"Peer went to sleep");
304 }
else if (
memcmp(data, woken_req, 2) == 0) {
307 }
else if (
memcmp(data, wakeup_req, 2) == 0) {
308 BT_DBG(
"Peer requested wakeup");
309 h5_link_control(hu, woken_req, 2);
312 BT_DBG(
"Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
319 static void h5_complete_rx_pkt(
struct hci_uart *hu)
321 struct h5 *h5 = hu->
priv;
322 const unsigned char *hdr = h5->
rx_skb->data;
349 h5_handle_internal_rx(hu);
356 static int h5_rx_crc(
struct hci_uart *hu,
unsigned char c)
358 struct h5 *h5 = hu->
priv;
360 h5_complete_rx_pkt(hu);
366 static int h5_rx_payload(
struct hci_uart *hu,
unsigned char c)
368 struct h5 *h5 = hu->
priv;
369 const unsigned char *hdr = h5->
rx_skb->data;
375 h5_complete_rx_pkt(hu);
382 static int h5_rx_3wire_hdr(
struct hci_uart *hu,
unsigned char c)
384 struct h5 *h5 = hu->
priv;
385 const unsigned char *hdr = h5->
rx_skb->data;
387 BT_DBG(
"%s rx: seq %u ack %u crc %u rel %u type %u len %u",
392 if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
393 BT_ERR(
"Invalid header checksum");
399 BT_ERR(
"Out-of-order packet arrived (%u != %u)",
407 BT_ERR(
"Non-link packet received in non-active state");
417 static int h5_rx_pkt_start(
struct hci_uart *hu,
unsigned char c)
419 struct h5 *h5 = hu->
priv;
429 BT_ERR(
"Can't allocate mem for new packet");
439 static int h5_rx_delimiter(
struct hci_uart *hu,
unsigned char c)
441 struct h5 *h5 = hu->
priv;
449 static void h5_unslip_one_byte(
struct h5 *h5,
unsigned char c)
468 BT_ERR(
"Invalid esc byte 0x%02hhx", c);
480 static void h5_reset_rx(
struct h5 *h5)
492 static int h5_recv(
struct hci_uart *hu,
void *data,
int count)
494 struct h5 *h5 = hu->
priv;
505 BT_ERR(
"Too short H5 packet");
510 h5_unslip_one_byte(h5, *ptr);
516 processed = h5->
rx_func(hu, *ptr);
529 struct h5 *h5 = hu->
priv;
531 if (skb->
len > 0xfff) {
532 BT_ERR(
"Packet too long (%u bytes)", skb->
len);
538 BT_ERR(
"Ignoring HCI data in non-active state");
543 switch (
bt_cb(skb)->pkt_type) {
554 BT_ERR(
"Unknown packet type %u",
bt_cb(skb)->pkt_type);
562 static void h5_slip_delim(
struct sk_buff *skb)
569 static void h5_slip_one_byte(
struct sk_buff *skb,
u8 c)
586 static bool valid_packet_type(
u8 type)
601 const u8 *data,
size_t len)
603 struct h5 *h5 = hu->
priv;
608 if (!valid_packet_type(pkt_type)) {
609 BT_ERR(
"Unknown packet type %u", pkt_type);
619 nskb = alloc_skb((len + 6) * 2 + 2,
GFP_ATOMIC);
637 hdr[1] = pkt_type | ((len & 0x0f) << 4);
639 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
641 BT_DBG(
"%s tx: seq %u ack %u crc %u rel %u type %u len %u",
646 for (i = 0; i < 4; i++)
647 h5_slip_one_byte(nskb, hdr[i]);
649 for (i = 0; i <
len; i++)
650 h5_slip_one_byte(nskb, data[i]);
659 struct h5 *h5 = hu->
priv;
663 if (h5->
sleep != H5_AWAKE) {
664 const unsigned char wakeup_req[] = { 0x05, 0xfa };
666 if (h5->
sleep == H5_WAKING_UP)
669 h5->
sleep = H5_WAKING_UP;
670 BT_DBG(
"Sending wakeup request");
677 nskb = h5_prepare_pkt(hu,
bt_cb(skb)->pkt_type,
685 BT_ERR(
"Could not dequeue pkt because alloc_skb failed");
694 nskb = h5_prepare_pkt(hu,
bt_cb(skb)->pkt_type,
697 __skb_queue_tail(&h5->
unack, skb);
699 spin_unlock_irqrestore(&h5->
unack.lock, flags);
704 BT_ERR(
"Could not dequeue pkt because alloc_skb failed");
708 spin_unlock_irqrestore(&h5->
unack.lock, flags);
716 static int h5_flush(
struct hci_uart *hu)
727 .enqueue = h5_enqueue,
728 .dequeue = h5_dequeue,
737 BT_INFO(
"HCI Three-wire UART (H5) protocol initialized");
739 BT_ERR(
"HCI Three-wire UART (H5) protocol init failed");