1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Generic Bluetooth HCI UART driver
6 * Copyright (C) 2015-2018 Intel Corporation
9 #include <linux/unaligned.h>
12 u8 type
; /* Packet type */
13 u8 hlen
; /* Header length */
14 u8 loff
; /* Data length offset in header */
15 u8 lsize
; /* Data length field size */
16 u16 maxlen
; /* Max overall packet length */
17 int (*recv
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
21 .type = HCI_ACLDATA_PKT, \
22 .hlen = HCI_ACL_HDR_SIZE, \
25 .maxlen = HCI_MAX_FRAME_SIZE \
28 .type = HCI_SCODATA_PKT, \
29 .hlen = HCI_SCO_HDR_SIZE, \
32 .maxlen = HCI_MAX_SCO_SIZE
34 #define H4_RECV_EVENT \
35 .type = HCI_EVENT_PKT, \
36 .hlen = HCI_EVENT_HDR_SIZE, \
39 .maxlen = HCI_MAX_EVENT_SIZE
42 .type = HCI_ISODATA_PKT, \
43 .hlen = HCI_ISO_HDR_SIZE, \
46 .maxlen = HCI_MAX_FRAME_SIZE
48 static inline struct sk_buff
*h4_recv_buf(struct hci_dev
*hdev
,
50 const unsigned char *buffer
,
52 const struct h4_recv_pkt
*pkts
,
55 /* Check for error from previous call */
63 for (i
= 0; i
< pkts_count
; i
++) {
64 if (buffer
[0] != (&pkts
[i
])->type
)
67 skb
= bt_skb_alloc((&pkts
[i
])->maxlen
,
70 return ERR_PTR(-ENOMEM
);
72 hci_skb_pkt_type(skb
) = (&pkts
[i
])->type
;
73 hci_skb_expect(skb
) = (&pkts
[i
])->hlen
;
77 /* Check for invalid packet type */
79 return ERR_PTR(-EILSEQ
);
85 len
= min_t(uint
, hci_skb_expect(skb
) - skb
->len
, count
);
86 skb_put_data(skb
, buffer
, len
);
91 /* Check for partial packet */
92 if (skb
->len
< hci_skb_expect(skb
))
95 for (i
= 0; i
< pkts_count
; i
++) {
96 if (hci_skb_pkt_type(skb
) == (&pkts
[i
])->type
)
100 if (i
>= pkts_count
) {
102 return ERR_PTR(-EILSEQ
);
105 if (skb
->len
== (&pkts
[i
])->hlen
) {
108 switch ((&pkts
[i
])->lsize
) {
110 /* No variable data length */
114 /* Single octet variable length */
115 dlen
= skb
->data
[(&pkts
[i
])->loff
];
116 hci_skb_expect(skb
) += dlen
;
118 if (skb_tailroom(skb
) < dlen
) {
120 return ERR_PTR(-EMSGSIZE
);
124 /* Double octet variable length */
125 dlen
= get_unaligned_le16(skb
->data
+
127 hci_skb_expect(skb
) += dlen
;
129 if (skb_tailroom(skb
) < dlen
) {
131 return ERR_PTR(-EMSGSIZE
);
135 /* Unsupported variable length */
137 return ERR_PTR(-EILSEQ
);
141 /* No more data, complete frame */
142 (&pkts
[i
])->recv(hdev
, skb
);
147 (&pkts
[i
])->recv(hdev
, skb
);