aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/bluetooth
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2015-04-06 02:44:59 -0400
committerMarcel Holtmann <marcel@holtmann.org>2015-04-07 12:48:21 -0400
commit79b8df9362e8bd1951e1fddbd65ca87af8df52c8 (patch)
treeb34a614b874924bbdb49dddfacb5fe2636d3a484 /drivers/bluetooth
parent9a0bb57d2d08f1923aeef3af1877ec0b6f3209ba (diff)
Bluetooth: hci_uart: Provide generic H:4 receive framework
Future H:4 based UART drivers require custom packet types and custom receive functions. To support this, extended the h4_recv_buf function with a packet definition table. For the default H:4 packets types of ACL data, SCO data and events, provide helpers to reduce the amount of code duplication. Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Diffstat (limited to 'drivers/bluetooth')
-rw-r--r--drivers/bluetooth/hci_ath.c9
-rw-r--r--drivers/bluetooth/hci_bcm.c9
-rw-r--r--drivers/bluetooth/hci_h4.c119
-rw-r--r--drivers/bluetooth/hci_uart.h33
4 files changed, 110 insertions, 60 deletions
diff --git a/drivers/bluetooth/hci_ath.c b/drivers/bluetooth/hci_ath.c
index 54af95c95b07..1b3f8647ea2f 100644
--- a/drivers/bluetooth/hci_ath.c
+++ b/drivers/bluetooth/hci_ath.c
@@ -190,12 +190,19 @@ static struct sk_buff *ath_dequeue(struct hci_uart *hu)
190 return skb_dequeue(&ath->txq); 190 return skb_dequeue(&ath->txq);
191} 191}
192 192
193static const struct h4_recv_pkt ath_recv_pkts[] = {
194 { H4_RECV_ACL, .recv = hci_recv_frame },
195 { H4_RECV_SCO, .recv = hci_recv_frame },
196 { H4_RECV_EVENT, .recv = hci_recv_frame },
197};
198
193/* Recv data */ 199/* Recv data */
194static int ath_recv(struct hci_uart *hu, const void *data, int count) 200static int ath_recv(struct hci_uart *hu, const void *data, int count)
195{ 201{
196 struct ath_struct *ath = hu->priv; 202 struct ath_struct *ath = hu->priv;
197 203
198 ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count); 204 ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
205 ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
199 if (IS_ERR(ath->rx_skb)) { 206 if (IS_ERR(ath->rx_skb)) {
200 int err = PTR_ERR(ath->rx_skb); 207 int err = PTR_ERR(ath->rx_skb);
201 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); 208 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
diff --git a/drivers/bluetooth/hci_bcm.c b/drivers/bluetooth/hci_bcm.c
index fe1905c4beb2..1ec0b4a5ffa6 100644
--- a/drivers/bluetooth/hci_bcm.c
+++ b/drivers/bluetooth/hci_bcm.c
@@ -86,6 +86,12 @@ static int bcm_setup(struct hci_uart *hu)
86 return btbcm_setup_patchram(hu->hdev); 86 return btbcm_setup_patchram(hu->hdev);
87} 87}
88 88
89static const struct h4_recv_pkt bcm_recv_pkts[] = {
90 { H4_RECV_ACL, .recv = hci_recv_frame },
91 { H4_RECV_SCO, .recv = hci_recv_frame },
92 { H4_RECV_EVENT, .recv = hci_recv_frame },
93};
94
89static int bcm_recv(struct hci_uart *hu, const void *data, int count) 95static int bcm_recv(struct hci_uart *hu, const void *data, int count)
90{ 96{
91 struct bcm_data *bcm = hu->priv; 97 struct bcm_data *bcm = hu->priv;
@@ -93,7 +99,8 @@ static int bcm_recv(struct hci_uart *hu, const void *data, int count)
93 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 99 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
94 return -EUNATCH; 100 return -EUNATCH;
95 101
96 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count); 102 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
103 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
97 if (IS_ERR(bcm->rx_skb)) { 104 if (IS_ERR(bcm->rx_skb)) {
98 int err = PTR_ERR(bcm->rx_skb); 105 int err = PTR_ERR(bcm->rx_skb);
99 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); 106 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
diff --git a/drivers/bluetooth/hci_h4.c b/drivers/bluetooth/hci_h4.c
index 07f5f7a21961..f7190f01e135 100644
--- a/drivers/bluetooth/hci_h4.c
+++ b/drivers/bluetooth/hci_h4.c
@@ -40,6 +40,7 @@
40#include <linux/signal.h> 40#include <linux/signal.h>
41#include <linux/ioctl.h> 41#include <linux/ioctl.h>
42#include <linux/skbuff.h> 42#include <linux/skbuff.h>
43#include <asm/unaligned.h>
43 44
44#include <net/bluetooth/bluetooth.h> 45#include <net/bluetooth/bluetooth.h>
45#include <net/bluetooth/hci_core.h> 46#include <net/bluetooth/hci_core.h>
@@ -113,6 +114,12 @@ static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
113 return 0; 114 return 0;
114} 115}
115 116
117static const struct h4_recv_pkt h4_recv_pkts[] = {
118 { H4_RECV_ACL, .recv = hci_recv_frame },
119 { H4_RECV_SCO, .recv = hci_recv_frame },
120 { H4_RECV_EVENT, .recv = hci_recv_frame },
121};
122
116/* Recv data */ 123/* Recv data */
117static int h4_recv(struct hci_uart *hu, const void *data, int count) 124static int h4_recv(struct hci_uart *hu, const void *data, int count)
118{ 125{
@@ -121,7 +128,8 @@ static int h4_recv(struct hci_uart *hu, const void *data, int count)
121 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 128 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
122 return -EUNATCH; 129 return -EUNATCH;
123 130
124 h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count); 131 h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count,
132 h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
125 if (IS_ERR(h4->rx_skb)) { 133 if (IS_ERR(h4->rx_skb)) {
126 int err = PTR_ERR(h4->rx_skb); 134 int err = PTR_ERR(h4->rx_skb);
127 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); 135 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
@@ -159,96 +167,93 @@ int __exit h4_deinit(void)
159} 167}
160 168
161struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb, 169struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
162 const unsigned char *buffer, int count) 170 const unsigned char *buffer, int count,
171 const struct h4_recv_pkt *pkts, int pkts_count)
163{ 172{
164 while (count) { 173 while (count) {
165 int len; 174 int i, len;
166 175
167 if (!skb) { 176 if (!skb) {
168 switch (buffer[0]) { 177 for (i = 0; i < pkts_count; i++) {
169 case HCI_ACLDATA_PKT: 178 if (buffer[0] != (&pkts[i])->type)
170 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, 179 continue;
171 GFP_ATOMIC);
172 if (!skb)
173 return ERR_PTR(-ENOMEM);
174 180
175 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 181 skb = bt_skb_alloc((&pkts[i])->maxlen,
176 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
177 break;
178 case HCI_SCODATA_PKT:
179 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE,
180 GFP_ATOMIC); 182 GFP_ATOMIC);
181 if (!skb) 183 if (!skb)
182 return ERR_PTR(-ENOMEM); 184 return ERR_PTR(-ENOMEM);
183 185
184 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 186 bt_cb(skb)->pkt_type = (&pkts[i])->type;
185 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE; 187 bt_cb(skb)->expect = (&pkts[i])->hlen;
186 break; 188 break;
187 case HCI_EVENT_PKT: 189 }
188 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE,
189 GFP_ATOMIC);
190 if (!skb)
191 return ERR_PTR(-ENOMEM);
192 190
193 bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 191 /* Check for invalid packet type */
194 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE; 192 if (!skb)
195 break;
196 default:
197 return ERR_PTR(-EILSEQ); 193 return ERR_PTR(-EILSEQ);
198 }
199 194
200 count -= 1; 195 count -= 1;
201 buffer += 1; 196 buffer += 1;
202 } 197 }
203 198
204 len = min_t(uint, bt_cb(skb)->expect, count); 199 len = min_t(uint, bt_cb(skb)->expect - skb->len, count);
205 memcpy(skb_put(skb, len), buffer, len); 200 memcpy(skb_put(skb, len), buffer, len);
206 201
207 count -= len; 202 count -= len;
208 buffer += len; 203 buffer += len;
209 bt_cb(skb)->expect -= len;
210 204
211 switch (bt_cb(skb)->pkt_type) { 205 /* Check for partial packet */
212 case HCI_ACLDATA_PKT: 206 if (skb->len < bt_cb(skb)->expect)
213 if (skb->len == HCI_ACL_HDR_SIZE) { 207 continue;
214 __le16 dlen = hci_acl_hdr(skb)->dlen; 208
209 for (i = 0; i < pkts_count; i++) {
210 if (bt_cb(skb)->pkt_type == (&pkts[i])->type)
211 break;
212 }
213
214 if (i >= pkts_count) {
215 kfree_skb(skb);
216 return ERR_PTR(-EILSEQ);
217 }
215 218
216 /* Complete ACL header */ 219 if (skb->len == (&pkts[i])->hlen) {
217 bt_cb(skb)->expect = __le16_to_cpu(dlen); 220 u16 dlen;
218 221
219 if (skb_tailroom(skb) < bt_cb(skb)->expect) { 222 switch ((&pkts[i])->lsize) {
220 kfree_skb(skb); 223 case 0:
221 return ERR_PTR(-EMSGSIZE); 224 /* No variable data length */
222 } 225 (&pkts[i])->recv(hdev, skb);
223 } 226 skb = NULL;
224 break; 227 break;
225 case HCI_SCODATA_PKT: 228 case 1:
226 if (skb->len == HCI_SCO_HDR_SIZE) { 229 /* Single octet variable length */
227 /* Complete SCO header */ 230 dlen = skb->data[(&pkts[i])->loff];
228 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen; 231 bt_cb(skb)->expect += dlen;
229 232
230 if (skb_tailroom(skb) < bt_cb(skb)->expect) { 233 if (skb_tailroom(skb) < dlen) {
231 kfree_skb(skb); 234 kfree_skb(skb);
232 return ERR_PTR(-EMSGSIZE); 235 return ERR_PTR(-EMSGSIZE);
233 } 236 }
234 } 237 break;
235 break; 238 case 2:
236 case HCI_EVENT_PKT: 239 /* Double octet variable length */
237 if (skb->len == HCI_EVENT_HDR_SIZE) { 240 dlen = get_unaligned_le16(skb->data +
238 /* Complete event header */ 241 (&pkts[i])->loff);
239 bt_cb(skb)->expect = hci_event_hdr(skb)->plen; 242 bt_cb(skb)->expect += dlen;
240 243
241 if (skb_tailroom(skb) < bt_cb(skb)->expect) { 244 if (skb_tailroom(skb) < dlen) {
242 kfree_skb(skb); 245 kfree_skb(skb);
243 return ERR_PTR(-EMSGSIZE); 246 return ERR_PTR(-EMSGSIZE);
244 } 247 }
248 break;
249 default:
250 /* Unsupported variable length */
251 kfree_skb(skb);
252 return ERR_PTR(-EILSEQ);
245 } 253 }
246 break; 254 } else {
247 }
248
249 if (bt_cb(skb)->expect == 0) {
250 /* Complete frame */ 255 /* Complete frame */
251 hci_recv_frame(hdev, skb); 256 (&pkts[i])->recv(hdev, skb);
252 skb = NULL; 257 skb = NULL;
253 } 258 }
254 } 259 }
diff --git a/drivers/bluetooth/hci_uart.h b/drivers/bluetooth/hci_uart.h
index a3108abce5d3..d1fa6263f7c2 100644
--- a/drivers/bluetooth/hci_uart.h
+++ b/drivers/bluetooth/hci_uart.h
@@ -101,8 +101,39 @@ int hci_uart_init_ready(struct hci_uart *hu);
101int h4_init(void); 101int h4_init(void);
102int h4_deinit(void); 102int h4_deinit(void);
103 103
104struct h4_recv_pkt {
105 u8 type; /* Packet type */
106 u8 hlen; /* Header length */
107 u8 loff; /* Data length offset in header */
108 u8 lsize; /* Data length field size */
109 u16 maxlen; /* Max overall packet length */
110 int (*recv)(struct hci_dev *hdev, struct sk_buff *skb);
111};
112
113#define H4_RECV_ACL \
114 .type = HCI_ACLDATA_PKT, \
115 .hlen = HCI_ACL_HDR_SIZE, \
116 .loff = 2, \
117 .lsize = 2, \
118 .maxlen = HCI_MAX_FRAME_SIZE \
119
120#define H4_RECV_SCO \
121 .type = HCI_SCODATA_PKT, \
122 .hlen = HCI_SCO_HDR_SIZE, \
123 .loff = 2, \
124 .lsize = 1, \
125 .maxlen = HCI_MAX_SCO_SIZE
126
127#define H4_RECV_EVENT \
128 .type = HCI_EVENT_PKT, \
129 .hlen = HCI_EVENT_HDR_SIZE, \
130 .loff = 1, \
131 .lsize = 1, \
132 .maxlen = HCI_MAX_EVENT_SIZE
133
104struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb, 134struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
105 const unsigned char *buffer, int count); 135 const unsigned char *buffer, int count,
136 const struct h4_recv_pkt *pkts, int pkts_count);
106#endif 137#endif
107 138
108#ifdef CONFIG_BT_HCIUART_BCSP 139#ifdef CONFIG_BT_HCIUART_BCSP