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_qualcomm.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2005-2010 Marcel Holtmann <[email protected]>
6
* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
7
*
8
*
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
13
*
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
18
*
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
*
23
*/
24
25
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27
#endif
28
29
#include <stdio.h>
30
#include <errno.h>
31
#include <fcntl.h>
32
#include <unistd.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <signal.h>
36
#include <syslog.h>
37
#include <termios.h>
38
#include <time.h>
39
#include <sys/time.h>
40
#include <sys/poll.h>
41
#include <sys/param.h>
42
#include <sys/ioctl.h>
43
44
#include <bluetooth/bluetooth.h>
45
#include <bluetooth/hci.h>
46
#include <bluetooth/hci_lib.h>
47
48
#include "hciattach.h"
49
50
#define FAILIF(x, args...) do { \
51
if (x) { \
52
fprintf(stderr, ##args); \
53
return -1; \
54
} \
55
} while (0)
56
57
typedef struct {
58
uint8_t uart_prefix;
59
hci_event_hdr hci_hdr;
60
evt_cmd_complete cmd_complete;
61
uint8_t status;
62
uint8_t data[16];
63
} __attribute__((packed)) command_complete_t;
64
65
static int read_command_complete(int fd,
66
unsigned short opcode,
67
unsigned char len)
68
{
69
command_complete_t resp;
70
unsigned char vsevent[512];
71
int n;
72
73
/* Read reply. */
74
n = read_hci_event(fd, vsevent, sizeof(vsevent));
75
FAILIF(n < 0, "Failed to read response");
76
77
FAILIF(vsevent[1] != 0xFF, "Failed to read response");
78
79
n = read_hci_event(fd, (unsigned char *)&resp, sizeof(resp));
80
FAILIF(n < 0, "Failed to read response");
81
82
/* event must be event-complete */
83
FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE,
84
"Error in response: not a cmd-complete event, "
85
"but 0x%02x!\n", resp.hci_hdr.evt);
86
87
FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
88
"Error in response: plen is not >= 4, but 0x%02x!\n",
89
resp.hci_hdr.plen);
90
91
/* cmd-complete event: opcode */
92
FAILIF(resp.cmd_complete.opcode != 0,
93
"Error in response: opcode is 0x%04x, not 0!",
94
resp.cmd_complete.opcode);
95
96
return resp.status == 0 ? 0 : -1;
97
}
98
99
static int qualcomm_load_firmware(int fd, const char *firmware, const char *bdaddr_s)
100
{
101
102
int fw = open(firmware, O_RDONLY);
103
104
fprintf(stdout, "Opening firmware file: %s\n", firmware);
105
106
FAILIF(fw < 0,
107
"Could not open firmware file %s: %s (%d).\n",
108
firmware, strerror(errno), errno);
109
110
fprintf(stdout, "Uploading firmware...\n");
111
do {
112
/* Read each command and wait for a response. */
113
unsigned char data[1024];
114
unsigned char cmdp[1 + sizeof(hci_command_hdr)];
115
hci_command_hdr *cmd = (hci_command_hdr *) (cmdp + 1);
116
int nr;
117
118
nr = read(fw, cmdp, sizeof(cmdp));
119
if (!nr)
120
break;
121
122
FAILIF(nr != sizeof(cmdp),
123
"Could not read H4 + HCI header!\n");
124
FAILIF(*cmdp != HCI_COMMAND_PKT,
125
"Command is not an H4 command packet!\n");
126
127
FAILIF(read(fw, data, cmd->plen) != cmd->plen,
128
"Could not read %d bytes of data \
129
for command with opcode %04x!\n",
130
cmd->plen, cmd->opcode);
131
132
if ((data[0] == 1) && (data[1] == 2) && (data[2] == 6)) {
133
bdaddr_t bdaddr;
134
if (bdaddr_s != NULL) {
135
str2ba(bdaddr_s, &bdaddr);
136
memcpy(&data[3], &bdaddr, sizeof(bdaddr_t));
137
}
138
}
139
140
{
141
int nw;
142
struct iovec iov_cmd[2];
143
iov_cmd[0].iov_base = cmdp;
144
iov_cmd[0].iov_len = sizeof(cmdp);
145
iov_cmd[1].iov_base = data;
146
iov_cmd[1].iov_len = cmd->plen;
147
nw = writev(fd, iov_cmd, 2);
148
FAILIF(nw != (int) sizeof(cmdp) + cmd->plen,
149
"Could not send entire command \
150
(sent only %d bytes)!\n",
151
nw);
152
}
153
154
/* Wait for response */
155
if (read_command_complete(fd, cmd->opcode, cmd->plen) < 0)
156
return -1;
157
} while (1);
158
fprintf(stdout, "Firmware upload successful.\n");
159
160
close(fw);
161
162
return 0;
163
}
164
165
int qualcomm_init(int fd, int speed, struct termios *ti, const char *bdaddr)
166
{
167
struct timespec tm = {0, 50000};
168
char cmd[5];
169
unsigned char resp[100]; /* Response */
170
char fw[100];
171
int n;
172
173
memset(resp, 0, 100);
174
175
/* Get Manufacturer and LMP version */
176
cmd[0] = HCI_COMMAND_PKT;
177
cmd[1] = 0x01;
178
cmd[2] = 0x10;
179
cmd[3] = 0x00;
180
181
do {
182
n = write(fd, cmd, 4);
183
if (n < 4) {
184
perror("Failed to write init command");
185
return -1;
186
}
187
188
/* Read reply. */
189
if (read_hci_event(fd, resp, 100) < 0) {
190
perror("Failed to read init response");
191
return -1;
192
}
193
194
/* Wait for command complete event for our Opcode */
195
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
196
197
/* Verify manufacturer */
198
if ((resp[11] & 0xFF) != 0x1d)
199
fprintf(stderr,
200
"WARNING : module's manufacturer is not Qualcomm\n");
201
202
/* Print LMP version */
203
fprintf(stderr,
204
"Qualcomm module LMP version : 0x%02x\n", resp[10] & 0xFF);
205
206
/* Print LMP subversion */
207
{
208
unsigned short lmp_subv = resp[13] | (resp[14] << 8);
209
210
fprintf(stderr, "Qualcomm module LMP sub-version : 0x%04x\n",
211
lmp_subv);
212
}
213
214
/* Get SoC type */
215
cmd[0] = HCI_COMMAND_PKT;
216
cmd[1] = 0x00;
217
cmd[2] = 0xFC;
218
cmd[3] = 0x01;
219
cmd[4] = 0x06;
220
221
do {
222
n = write(fd, cmd, 5);
223
if (n < 5) {
224
perror("Failed to write vendor init command");
225
return -1;
226
}
227
228
/* Read reply. */
229
if ((n = read_hci_event(fd, resp, 100)) < 0) {
230
perror("Failed to read vendor init response");
231
return -1;
232
}
233
234
} while (resp[3] != 0 && resp[4] != 2);
235
236
snprintf(fw, sizeof(fw), "/etc/firmware/%c%c%c%c%c%c_%c%c%c%c.bin",
237
resp[18], resp[19], resp[20], resp[21],
238
resp[22], resp[23],
239
resp[32], resp[33], resp[34], resp[35]);
240
241
/* Wait for command complete event for our Opcode */
242
if (read_hci_event(fd, resp, 100) < 0) {
243
perror("Failed to read init response");
244
return -1;
245
}
246
247
qualcomm_load_firmware(fd, fw, bdaddr);
248
249
/* Reset */
250
cmd[0] = HCI_COMMAND_PKT;
251
cmd[1] = 0x03;
252
cmd[2] = 0x0C;
253
cmd[3] = 0x00;
254
255
do {
256
n = write(fd, cmd, 4);
257
if (n < 4) {
258
perror("Failed to write reset command");
259
return -1;
260
}
261
262
/* Read reply. */
263
if ((n = read_hci_event(fd, resp, 100)) < 0) {
264
perror("Failed to read reset response");
265
return -1;
266
}
267
268
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
269
270
nanosleep(&tm, NULL);
271
272
return 0;
273
}
274
275