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_tialt.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2005-2010 Marcel Holtmann <[email protected]>
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 <errno.h>
30
#include <fcntl.h>
31
#include <unistd.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <signal.h>
35
#include <syslog.h>
36
#include <termios.h>
37
#include <time.h>
38
#include <sys/time.h>
39
#include <sys/poll.h>
40
#include <sys/param.h>
41
#include <sys/ioctl.h>
42
43
#include <bluetooth/bluetooth.h>
44
#include <bluetooth/hci.h>
45
#include <bluetooth/hci_lib.h>
46
47
#include "hciattach.h"
48
49
#define FAILIF(x, args...) do { \
50
if (x) { \
51
fprintf(stderr, ##args); \
52
return -1; \
53
} \
54
} while(0)
55
56
typedef struct {
57
uint8_t uart_prefix;
58
hci_event_hdr hci_hdr;
59
evt_cmd_complete cmd_complete;
60
uint8_t status;
61
uint8_t data[16];
62
} __attribute__((packed)) command_complete_t;
63
64
static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
65
command_complete_t resp;
66
/* Read reply. */
67
FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
68
"Failed to read response");
69
70
/* Parse speed-change reply */
71
FAILIF(resp.uart_prefix != HCI_EVENT_PKT,
72
"Error in response: not an event packet, but 0x%02x!\n",
73
resp.uart_prefix);
74
75
FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE, /* event must be event-complete */
76
"Error in response: not a cmd-complete event, "
77
"but 0x%02x!\n", resp.hci_hdr.evt);
78
79
FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
80
"Error in response: plen is not >= 4, but 0x%02x!\n",
81
resp.hci_hdr.plen);
82
83
/* cmd-complete event: opcode */
84
FAILIF(resp.cmd_complete.opcode != (uint16_t)opcode,
85
"Error in response: opcode is 0x%04x, not 0x%04x!",
86
resp.cmd_complete.opcode, opcode);
87
88
return resp.status == 0 ? 0 : -1;
89
}
90
91
typedef struct {
92
uint8_t uart_prefix;
93
hci_command_hdr hci_hdr;
94
uint32_t speed;
95
} __attribute__((packed)) texas_speed_change_cmd_t;
96
97
static int texas_change_speed(int fd, uint32_t speed)
98
{
99
return 0;
100
}
101
102
static int texas_load_firmware(int fd, const char *firmware) {
103
104
int fw = open(firmware, O_RDONLY);
105
106
fprintf(stdout, "Opening firmware file: %s\n", firmware);
107
108
FAILIF(fw < 0,
109
"Could not open firmware file %s: %s (%d).\n",
110
firmware, strerror(errno), errno);
111
112
fprintf(stdout, "Uploading firmware...\n");
113
do {
114
/* Read each command and wait for a response. */
115
unsigned char data[1024];
116
unsigned char cmdp[1 + sizeof(hci_command_hdr)];
117
hci_command_hdr *cmd = (hci_command_hdr *)(cmdp + 1);
118
int nr;
119
nr = read(fw, cmdp, sizeof(cmdp));
120
if (!nr)
121
break;
122
FAILIF(nr != sizeof(cmdp), "Could not read H4 + HCI header!\n");
123
FAILIF(*cmdp != HCI_COMMAND_PKT, "Command is not an H4 command packet!\n");
124
125
FAILIF(read(fw, data, cmd->plen) != cmd->plen,
126
"Could not read %d bytes of data for command with opcode %04x!\n",
127
cmd->plen,
128
cmd->opcode);
129
130
{
131
int nw;
132
#if 0
133
fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
134
cmd->opcode,
135
cmd->plen);
136
#endif
137
struct iovec iov_cmd[2];
138
iov_cmd[0].iov_base = cmdp;
139
iov_cmd[0].iov_len = sizeof(cmdp);
140
iov_cmd[1].iov_base = data;
141
iov_cmd[1].iov_len = cmd->plen;
142
nw = writev(fd, iov_cmd, 2);
143
FAILIF(nw != (int) sizeof(cmd) + cmd->plen,
144
"Could not send entire command (sent only %d bytes)!\n",
145
nw);
146
}
147
148
/* Wait for response */
149
if (read_command_complete(fd,
150
cmd->opcode,
151
cmd->plen) < 0) {
152
return -1;
153
}
154
155
} while(1);
156
fprintf(stdout, "Firmware upload successful.\n");
157
158
close(fw);
159
return 0;
160
}
161
162
int texasalt_init(int fd, int speed, struct termios *ti)
163
{
164
struct timespec tm = {0, 50000};
165
char cmd[4];
166
unsigned char resp[100]; /* Response */
167
int n;
168
169
memset(resp,'\0', 100);
170
171
/* It is possible to get software version with manufacturer specific
172
HCI command HCI_VS_TI_Version_Number. But the only thing you get more
173
is if this is point-to-point or point-to-multipoint module */
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 < 0) {
184
perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
185
return -1;
186
}
187
if (n < 4) {
188
fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
189
return -1;
190
}
191
192
/* Read reply. */
193
if (read_hci_event(fd, resp, 100) < 0) {
194
perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
195
return -1;
196
}
197
198
/* Wait for command complete event for our Opcode */
199
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
200
201
/* Verify manufacturer */
202
if ((resp[11] & 0xFF) != 0x0d)
203
fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
204
205
/* Print LMP version */
206
fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
207
208
/* Print LMP subversion */
209
{
210
unsigned short lmp_subv = resp[13] | (resp[14] << 8);
211
unsigned short brf_chip = (lmp_subv & 0x7c00) >> 10;
212
static const char *c_brf_chip[8] = {
213
"unknown",
214
"unknown",
215
"brf6100",
216
"brf6150",
217
"brf6300",
218
"brf6350",
219
"unknown",
220
"wl1271"
221
};
222
char fw[100];
223
224
fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
225
226
fprintf(stderr,
227
"\tinternal version freeze: %d\n"
228
"\tsoftware version: %d\n"
229
"\tchip: %s (%d)\n",
230
lmp_subv & 0x7f,
231
((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
232
((brf_chip > 7) ? "unknown" : c_brf_chip[brf_chip]),
233
brf_chip);
234
235
sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
236
texas_load_firmware(fd, fw);
237
238
texas_change_speed(fd, speed);
239
}
240
nanosleep(&tm, NULL);
241
return 0;
242
}
243
244