CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/hcitools/hciattach_bcm43xx.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2014 Intel Corporation. All rights reserved.
6
*
7
*
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.
12
*
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.
17
*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
*
22
*/
23
24
#ifdef HAVE_CONFIG_H
25
#include <config.h>
26
#endif
27
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <unistd.h>
31
#include <termios.h>
32
#include <fcntl.h>
33
#include <unistd.h>
34
#include <errno.h>
35
#include <dirent.h>
36
#include <time.h>
37
38
#include <bluetooth/bluetooth.h>
39
#include <bluetooth/hci.h>
40
#include <bluetooth/hci_lib.h>
41
42
#include "hciattach.h"
43
44
#ifndef FIRMWARE_DIR
45
#define FIRMWARE_DIR "/etc/firmware"
46
#endif
47
48
#define FW_EXT ".hcd"
49
50
#define BCM43XX_CLOCK_48 1
51
#define BCM43XX_CLOCK_24 2
52
53
#define CMD_SUCCESS 0x00
54
55
#define CC_MIN_SIZE 7
56
57
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
58
59
static int bcm43xx_read_local_name(int fd, char *name, size_t size)
60
{
61
unsigned char cmd[] = { HCI_COMMAND_PKT, 0x14, 0x0C, 0x00 };
62
unsigned char *resp;
63
unsigned int name_len;
64
65
resp = malloc(size + CC_MIN_SIZE);
66
if (!resp)
67
return -1;
68
69
tcflush(fd, TCIOFLUSH);
70
71
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
72
fprintf(stderr, "Failed to write read local name command\n");
73
goto fail;
74
}
75
76
if (read_hci_event(fd, resp, size) < CC_MIN_SIZE) {
77
fprintf(stderr, "Failed to read local name, invalid HCI event\n");
78
goto fail;
79
}
80
81
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
82
fprintf(stderr, "Failed to read local name, command failure\n");
83
goto fail;
84
}
85
86
name_len = resp[2] - 1;
87
88
strncpy(name, (char *) &resp[7], MIN(name_len, size));
89
name[size - 1] = 0;
90
91
free(resp);
92
return 0;
93
94
fail:
95
free(resp);
96
return -1;
97
}
98
99
static int bcm43xx_reset(int fd)
100
{
101
unsigned char cmd[] = { HCI_COMMAND_PKT, 0x03, 0x0C, 0x00 };
102
unsigned char resp[CC_MIN_SIZE];
103
104
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
105
fprintf(stderr, "Failed to write reset command\n");
106
return -1;
107
}
108
109
if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {
110
fprintf(stderr, "Failed to reset chip, invalid HCI event\n");
111
return -1;
112
}
113
114
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
115
fprintf(stderr, "Failed to reset chip, command failure\n");
116
return -1;
117
}
118
119
return 0;
120
}
121
122
static int bcm43xx_set_bdaddr(int fd, const char *bdaddr)
123
{
124
unsigned char cmd[] =
125
{ HCI_COMMAND_PKT, 0x01, 0xfc, 0x06, 0x00, 0x00,
126
0x00, 0x00, 0x00, 0x00 };
127
unsigned char resp[CC_MIN_SIZE];
128
129
printf("Set BDADDR UART: %s\n", bdaddr);
130
131
if (str2ba(bdaddr, (bdaddr_t *) (&cmd[4])) < 0) {
132
fprintf(stderr, "Incorrect bdaddr\n");
133
return -1;
134
}
135
136
tcflush(fd, TCIOFLUSH);
137
138
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
139
fprintf(stderr, "Failed to write set bdaddr command\n");
140
return -1;
141
}
142
143
if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {
144
fprintf(stderr, "Failed to set bdaddr, invalid HCI event\n");
145
return -1;
146
}
147
148
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
149
fprintf(stderr, "Failed to set bdaddr, command failure\n");
150
return -1;
151
}
152
153
return 0;
154
}
155
156
static int bcm43xx_set_speed(int fd, uint32_t speed)
157
{
158
unsigned char cmd[] =
159
{ HCI_COMMAND_PKT, 0x18, 0xfc, 0x06, 0x00, 0x00,
160
0x00, 0x00, 0x00, 0x00 };
161
unsigned char resp[CC_MIN_SIZE];
162
163
printf("Set Controller UART speed to %d bit/s\n", speed);
164
165
cmd[6] = (uint8_t) (speed);
166
cmd[7] = (uint8_t) (speed >> 8);
167
cmd[8] = (uint8_t) (speed >> 16);
168
cmd[9] = (uint8_t) (speed >> 24);
169
170
tcflush(fd, TCIOFLUSH);
171
172
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
173
fprintf(stderr, "Failed to write update baudrate command\n");
174
return -1;
175
}
176
177
if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {
178
fprintf(stderr, "Failed to update baudrate, invalid HCI event\n");
179
return -1;
180
}
181
182
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
183
fprintf(stderr, "Failed to update baudrate, command failure\n");
184
return -1;
185
}
186
187
return 0;
188
}
189
190
static int bcm43xx_set_clock(int fd, unsigned char clock)
191
{
192
unsigned char cmd[] = { HCI_COMMAND_PKT, 0x45, 0xfc, 0x01, 0x00 };
193
unsigned char resp[CC_MIN_SIZE];
194
195
printf("Set Controller clock (%d)\n", clock);
196
197
cmd[4] = clock;
198
199
tcflush(fd, TCIOFLUSH);
200
201
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
202
fprintf(stderr, "Failed to write update clock command\n");
203
return -1;
204
}
205
206
if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {
207
fprintf(stderr, "Failed to update clock, invalid HCI event\n");
208
return -1;
209
}
210
211
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
212
fprintf(stderr, "Failed to update clock, command failure\n");
213
return -1;
214
}
215
216
return 0;
217
}
218
219
static int bcm43xx_load_firmware(int fd, const char *fw)
220
{
221
unsigned char cmd[] = { HCI_COMMAND_PKT, 0x2e, 0xfc, 0x00 };
222
struct timespec tm_mode = { 0, 50000 };
223
struct timespec tm_ready = { 0, 2000000 };
224
unsigned char resp[CC_MIN_SIZE];
225
unsigned char tx_buf[1024];
226
int len, fd_fw, n;
227
228
printf("Flash firmware %s\n", fw);
229
230
fd_fw = open(fw, O_RDONLY);
231
if (fd_fw < 0) {
232
fprintf(stderr, "Unable to open firmware (%s)\n", fw);
233
return -1;
234
}
235
236
tcflush(fd, TCIOFLUSH);
237
238
if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {
239
fprintf(stderr, "Failed to write download mode command\n");
240
goto fail;
241
}
242
243
if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {
244
fprintf(stderr, "Failed to load firmware, invalid HCI event\n");
245
goto fail;
246
}
247
248
if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {
249
fprintf(stderr, "Failed to load firmware, command failure\n");
250
goto fail;
251
}
252
253
/* Wait 50ms to let the firmware placed in download mode */
254
nanosleep(&tm_mode, NULL);
255
256
tcflush(fd, TCIOFLUSH);
257
258
while ((n = read(fd_fw, &tx_buf[1], 3))) {
259
if (n < 0) {
260
fprintf(stderr, "Failed to read firmware\n");
261
goto fail;
262
}
263
264
tx_buf[0] = HCI_COMMAND_PKT;
265
266
len = tx_buf[3];
267
268
if (read(fd_fw, &tx_buf[4], len) < 0) {
269
fprintf(stderr, "Failed to read firmware\n");
270
goto fail;
271
}
272
273
if (write(fd, tx_buf, len + 4) != (len + 4)) {
274
fprintf(stderr, "Failed to write firmware\n");
275
goto fail;
276
}
277
278
read_hci_event(fd, resp, sizeof(resp));
279
tcflush(fd, TCIOFLUSH);
280
}
281
282
/* Wait for firmware ready */
283
nanosleep(&tm_ready, NULL);
284
285
close(fd_fw);
286
return 0;
287
288
fail:
289
close(fd_fw);
290
return -1;
291
}
292
293
static int bcm43xx_locate_patch(const char *dir_name,
294
const char *chip_name, char *location)
295
{
296
DIR *dir;
297
int ret = -1;
298
299
dir = opendir(dir_name);
300
if (!dir) {
301
fprintf(stderr, "Cannot open directory '%s': %s\n",
302
dir_name, strerror(errno));
303
return -1;
304
}
305
306
/* Recursively look for a BCM43XX*.hcd */
307
while (1) {
308
struct dirent *entry = readdir(dir);
309
if (!entry)
310
break;
311
312
if (entry->d_type & DT_DIR) {
313
char path[PATH_MAX];
314
315
if (!strcmp(entry->d_name, "..") || !strcmp(entry->d_name, "."))
316
continue;
317
318
snprintf(path, PATH_MAX, "%s/%s", dir_name, entry->d_name);
319
320
ret = bcm43xx_locate_patch(path, chip_name, location);
321
if (!ret)
322
break;
323
} else if (!strncmp(chip_name, entry->d_name, strlen(chip_name))) {
324
unsigned int name_len = strlen(entry->d_name);
325
size_t curs_ext = name_len - sizeof(FW_EXT) + 1;
326
327
if (curs_ext > name_len)
328
break;
329
330
if (strncmp(FW_EXT, &entry->d_name[curs_ext], sizeof(FW_EXT)))
331
break;
332
333
/* found */
334
snprintf(location, PATH_MAX, "%s/%s", dir_name, entry->d_name);
335
ret = 0;
336
break;
337
}
338
}
339
340
closedir(dir);
341
342
return ret;
343
}
344
345
int bcm43xx_init(int fd, int speed, struct termios *ti, const char *bdaddr)
346
{
347
char chip_name[20];
348
char fw_path[PATH_MAX];
349
350
printf("bcm43xx_init\n");
351
352
if (bcm43xx_reset(fd))
353
return -1;
354
355
if (bcm43xx_read_local_name(fd, chip_name, sizeof(chip_name)))
356
return -1;
357
358
if (bcm43xx_locate_patch(FIRMWARE_DIR, chip_name, fw_path)) {
359
fprintf(stderr, "Patch not found, continue anyway\n");
360
} else {
361
if (bcm43xx_load_firmware(fd, fw_path))
362
return -1;
363
364
if (bcm43xx_reset(fd))
365
return -1;
366
}
367
368
if (bdaddr)
369
bcm43xx_set_bdaddr(fd, bdaddr);
370
371
if (speed > 3000000 && bcm43xx_set_clock(fd, BCM43XX_CLOCK_48))
372
return -1;
373
374
if (bcm43xx_set_speed(fd, speed))
375
return -1;
376
377
return 0;
378
}
379
380