forked from espressif/esp-hosted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_bt.c
215 lines (171 loc) · 4.87 KB
/
esp_bt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Espressif Systems Wireless LAN device driver
*
* Copyright (C) 2015-2021 Espressif Systems (Shanghai) PTE LTD
*
* This software file (the "File") is distributed by Espressif Systems (Shanghai)
* PTE LTD under the terms of the GNU General Public License Version 2, June 1991
* (the "License"). You may use, redistribute and/or modify this File in
* accordance with the terms and conditions of the License, a copy of which
* is available by writing to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
* worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
* ARE EXPRESSLY DISCLAIMED. The License provides additional details about
* this warranty disclaimer.
*/
#include "esp_bt_api.h"
#include "esp_api.h"
#define INVALID_HDEV_BUS (0xff)
void esp_hci_update_tx_counter(struct hci_dev *hdev, u8 pkt_type, size_t len)
{
if (hdev) {
if (pkt_type == HCI_COMMAND_PKT) {
hdev->stat.cmd_tx++;
} else if (pkt_type == HCI_ACLDATA_PKT) {
hdev->stat.acl_tx++;
} else if (pkt_type == HCI_SCODATA_PKT) {
hdev->stat.sco_tx++;
}
hdev->stat.byte_tx += len;
}
}
void esp_hci_update_rx_counter(struct hci_dev *hdev, u8 pkt_type, size_t len)
{
if (hdev) {
if (pkt_type == HCI_EVENT_PKT) {
hdev->stat.evt_rx++;
} else if (pkt_type == HCI_ACLDATA_PKT) {
hdev->stat.acl_rx++;
} else if (pkt_type == HCI_SCODATA_PKT) {
hdev->stat.sco_rx++;
}
hdev->stat.byte_rx += len;
}
}
static int esp_bt_open(struct hci_dev *hdev)
{
return 0;
}
static int esp_bt_close(struct hci_dev *hdev)
{
return 0;
}
static int esp_bt_flush(struct hci_dev *hdev)
{
return 0;
}
static int esp_bt_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
{
struct esp_payload_header *hdr;
size_t total_len, len = skb->len;
int ret = 0;
struct esp_adapter *adapter = hci_get_drvdata(hdev);
struct sk_buff *new_skb;
if (!adapter) {
printk(KERN_ERR "%s: invalid args", __func__);
return -EINVAL;
}
total_len = len + sizeof(struct esp_payload_header);
if (skb_headroom(skb) < sizeof(struct esp_payload_header)) {
/* insufficent headroom to add payload header */
new_skb = skb_realloc_headroom(skb, sizeof(struct esp_payload_header));
if(!new_skb) {
printk(KERN_ERR "%s: Failed to allocate SKB", __func__);
dev_kfree_skb(skb);
hdev->stat.err_tx++;
return -ENOMEM;
}
dev_kfree_skb(skb);
skb = new_skb;
}
skb_push(skb, sizeof(struct esp_payload_header));
hdr = (struct esp_payload_header *) skb->data;
memset (hdr, 0, sizeof(struct esp_payload_header));
hdr->if_type = ESP_HCI_IF;
hdr->if_num = 0;
hdr->len = cpu_to_le16(len);
hdr->offset = cpu_to_le16(sizeof(struct esp_payload_header));
hdr->hci_pkt_type = hci_skb_pkt_type(skb);
ret = esp_send_packet(adapter, skb);
if (ret) {
hdev->stat.err_tx++;
} else {
esp_hci_update_tx_counter(hdev, hdr->hci_pkt_type, skb->len);
}
return 0;
}
static int esp_bt_setup(struct hci_dev *hdev)
{
return 0;
}
static int esp_bt_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
{
return 0;
}
int esp_deinit_bt(struct esp_adapter *adapter)
{
struct hci_dev *hdev = NULL;
if (!adapter || !adapter->hcidev)
return 0;
hdev = adapter->hcidev;
hci_unregister_dev(hdev);
hci_free_dev(hdev);
adapter->hcidev = NULL;
return 0;
}
int esp_init_bt(struct esp_adapter *adapter)
{
int ret = 0;
struct hci_dev *hdev = NULL;
if (!adapter) {
return -EINVAL;
}
if (adapter->hcidev) {
return -EEXIST;
}
hdev = hci_alloc_dev();
if (!hdev) {
BT_ERR("Can not allocate HCI device");
return -ENOMEM;
}
adapter->hcidev = hdev;
hci_set_drvdata(hdev, adapter);
hdev->bus = INVALID_HDEV_BUS;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19))
if (adapter->if_type == ESP_IF_TYPE_SDIO)
hdev->bus = HCI_SDIO;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
else if (adapter->if_type == ESP_IF_TYPE_SPI)
hdev->bus = HCI_SPI;
#endif
#endif
if (hdev->bus == INVALID_HDEV_BUS) {
if (adapter->if_type == ESP_IF_TYPE_SDIO) {
printk(KERN_ERR "%s: Kernel version does not support HCI over SDIO BUS\n",__func__);
} else if (adapter->if_type == ESP_IF_TYPE_SPI) {
printk(KERN_ERR "%s: Kernel version does not support HCI over SPI BUS\n",__func__);
} else {
printk(KERN_ERR "%s: HCI over expected BUS[%u] is not supported\n",__func__, adapter->if_type);
}
hci_free_dev(hdev);
adapter->hcidev = NULL;
return -EINVAL;
}
hdev->open = esp_bt_open;
hdev->close = esp_bt_close;
hdev->flush = esp_bt_flush;
hdev->send = esp_bt_send_frame;
hdev->setup = esp_bt_setup;
hdev->set_bdaddr = esp_bt_set_bdaddr;
hdev->dev_type = HCI_PRIMARY;
ret = hci_register_dev(hdev);
if (ret < 0) {
BT_ERR("Can not register HCI device");
hci_free_dev(hdev);
return -ENOMEM;
}
return 0;
}