3 * Generic Bluetooth HCI UART driver
5 * Copyright (C) 2015-2018 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 <asm/unaligned.h>
27 u8 type
; /* Packet type */
28 u8 hlen
; /* Header length */
29 u8 loff
; /* Data length offset in header */
30 u8 lsize
; /* Data length field size */
31 u16 maxlen
; /* Max overall packet length */
32 int (*recv
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
36 .type = HCI_ACLDATA_PKT, \
37 .hlen = HCI_ACL_HDR_SIZE, \
40 .maxlen = HCI_MAX_FRAME_SIZE \
43 .type = HCI_SCODATA_PKT, \
44 .hlen = HCI_SCO_HDR_SIZE, \
47 .maxlen = HCI_MAX_SCO_SIZE
49 #define H4_RECV_EVENT \
50 .type = HCI_EVENT_PKT, \
51 .hlen = HCI_EVENT_HDR_SIZE, \
54 .maxlen = HCI_MAX_EVENT_SIZE
56 static inline struct sk_buff
*h4_recv_buf(struct hci_dev
*hdev
,
58 const unsigned char *buffer
,
60 const struct h4_recv_pkt
*pkts
,
70 for (i
= 0; i
< pkts_count
; i
++) {
71 if (buffer
[0] != (&pkts
[i
])->type
)
74 skb
= bt_skb_alloc((&pkts
[i
])->maxlen
,
77 return ERR_PTR(-ENOMEM
);
79 hci_skb_pkt_type(skb
) = (&pkts
[i
])->type
;
80 hci_skb_expect(skb
) = (&pkts
[i
])->hlen
;
84 /* Check for invalid packet type */
86 return ERR_PTR(-EILSEQ
);
92 len
= min_t(uint
, hci_skb_expect(skb
) - skb
->len
, count
);
93 skb_put_data(skb
, buffer
, len
);
98 /* Check for partial packet */
99 if (skb
->len
< hci_skb_expect(skb
))
102 for (i
= 0; i
< pkts_count
; i
++) {
103 if (hci_skb_pkt_type(skb
) == (&pkts
[i
])->type
)
107 if (i
>= pkts_count
) {
109 return ERR_PTR(-EILSEQ
);
112 if (skb
->len
== (&pkts
[i
])->hlen
) {
115 switch ((&pkts
[i
])->lsize
) {
117 /* No variable data length */
121 /* Single octet variable length */
122 dlen
= skb
->data
[(&pkts
[i
])->loff
];
123 hci_skb_expect(skb
) += dlen
;
125 if (skb_tailroom(skb
) < dlen
) {
127 return ERR_PTR(-EMSGSIZE
);
131 /* Double octet variable length */
132 dlen
= get_unaligned_le16(skb
->data
+
134 hci_skb_expect(skb
) += dlen
;
136 if (skb_tailroom(skb
) < dlen
) {
138 return ERR_PTR(-EMSGSIZE
);
142 /* Unsupported variable length */
144 return ERR_PTR(-EILSEQ
);
148 /* No more data, complete frame */
149 (&pkts
[i
])->recv(hdev
, skb
);
154 (&pkts
[i
])->recv(hdev
, skb
);