3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
40
#include <sys/param.h>
41
#include <sys/ioctl.h>
42
#include <sys/socket.h>
45
#include <bluetooth/bluetooth.h>
46
#include <bluetooth/hci.h>
47
#include <bluetooth/hci_lib.h>
49
#define FAILIF(x, args...) do { \
51
fprintf(stderr, ##args); \
58
hci_event_hdr hci_hdr;
59
evt_cmd_complete cmd_complete;
62
} __attribute__((packed)) command_complete_t;
64
extern int read_hci_event(int fd, unsigned char* buf, int size);
66
static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
67
command_complete_t resp;
69
FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
70
"Failed to read response");
72
/* Parse speed-change reply */
73
FAILIF(resp.uart_prefix != HCI_EVENT_PKT,
74
"Error in response: not an event packet, but 0x%02x!\n",
77
FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE, /* event must be event-complete */
78
"Error in response: not a cmd-complete event, "
79
"but 0x%02x!\n", resp.hci_hdr.evt);
81
FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
82
"Error in response: plen is not >= 4, but 0x%02x!\n",
85
/* cmd-complete event: opcode */
86
FAILIF(resp.cmd_complete.opcode != (uint16_t)opcode,
87
"Error in response: opcode is 0x%04x, not 0x%04x!",
88
resp.cmd_complete.opcode, opcode);
90
return resp.status == 0 ? 0 : -1;
95
hci_command_hdr hci_hdr;
97
} __attribute__((packed)) texas_speed_change_cmd_t;
99
static int texas_change_speed(int fd, uint32_t speed) {
103
/* Send a speed-change request */
105
texas_speed_change_cmd_t cmd;
108
cmd.uart_prefix = HCI_COMMAND_PKT;
109
cmd.hci_hdr.opcode = 0xff36;
110
cmd.hci_hdr.plen = sizeof(uint32_t);
113
fprintf(stdout, "Setting speed to %d\n", speed);
114
n = write(fd, &cmd, sizeof(cmd));
116
perror("Failed to write speed-set command");
120
n = write(fd, &cmd, sizeof(cmd));
122
perror("Failed to write command.");
125
if (n < (int)sizeof(cmd)) {
126
fprintf(stderr, "Wanted to write %d bytes, could only write %d. "
127
"Stop\n", (int)sizeof(cmd), n);
131
/* Parse speed-change reply */
132
if (read_command_complete(fd, 0xff36, 4) < 0) {
135
fprintf(stdout, "Speed changed to %d.\n", speed);
138
static int texas_load_firmware(int fd, const char *firmware) {
140
fprintf(stdout, "Opening firmware file: %s\n", firmware);
141
int fw = open(firmware, O_RDONLY);
143
"Could not open firmware file %s: %s (%d).\n",
144
firmware, strerror(errno), errno);
146
fprintf(stdout, "Uploading firmware...\n");
149
/* Read each command and wait for a response. */
150
unsigned char cmdp[1 + sizeof(hci_command_hdr)];
151
nr = read(fw, cmdp, sizeof(cmdp));
154
hci_command_hdr *cmd = (hci_command_hdr *)(cmdp + 1);
155
FAILIF(nr != sizeof(cmdp), "Could not read H4 + HCI header!\n");
156
FAILIF(*cmdp != HCI_COMMAND_PKT, "Command is not an H4 command packet!\n");
158
unsigned char data[1024];
159
FAILIF(read(fw, data, cmd->plen) != cmd->plen,
160
"Could not read %d bytes of data for command with opcode %04x!\n",
166
fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
170
struct iovec iov_cmd[2];
171
iov_cmd[0].iov_base = cmdp;
172
iov_cmd[0].iov_len = sizeof(cmdp);
173
iov_cmd[1].iov_base = data;
174
iov_cmd[1].iov_len = cmd->plen;
175
int nw = writev(fd, iov_cmd, 2);
176
FAILIF(nw != sizeof(cmd) + cmd->plen,
177
"Could not send entire command (sent only %d bytes)!\n",
181
/* Wait for response */
182
if (read_command_complete(fd,
189
fprintf(stdout, "Firmware upload successful.\n");
195
int texasalt_init(int fd, int speed, struct termios *ti)
197
struct timespec tm = {0, 50000};
199
unsigned char resp[100]; /* Response */
202
memset(resp,'\0', 100);
204
/* It is possible to get software version with manufacturer specific
205
HCI command HCI_VS_TI_Version_Number. But the only thing you get more
206
is if this is point-to-point or point-to-multipoint module */
208
/* Get Manufacturer and LMP version */
209
cmd[0] = HCI_COMMAND_PKT;
215
n = write(fd, cmd, 4);
217
perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
221
fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
226
if (read_hci_event(fd, resp, 100) < 0) {
227
perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
231
/* Wait for command complete event for our Opcode */
232
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
234
/* Verify manufacturer */
235
if ((resp[11] & 0xFF) != 0x0d)
236
fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
238
/* Print LMP version */
239
fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
241
/* Print LMP subversion */
243
unsigned short lmp_subv = resp[13] | (resp[14] << 8);
244
unsigned short brf_chip = (lmp_subv & 0x7c00) >> 10;
245
static const char *c_brf_chip[5] = {
253
fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
256
"\tinternal version freeze: %d\n"
257
"\tsoftware version: %d\n"
260
((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
261
((brf_chip > 4) ? "unknown" : c_brf_chip[brf_chip]),
265
sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
266
texas_load_firmware(fd, fw);
268
texas_change_speed(fd, speed);
270
nanosleep(&tm, NULL);