3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2005-2010 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>
43
#include <bluetooth/bluetooth.h>
44
#include <bluetooth/hci.h>
45
#include <bluetooth/hci_lib.h>
47
#include "hciattach.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
static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
65
command_complete_t resp;
67
FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
68
"Failed to read response");
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",
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);
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",
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);
88
return resp.status == 0 ? 0 : -1;
93
hci_command_hdr hci_hdr;
95
} __attribute__((packed)) texas_speed_change_cmd_t;
97
static int texas_change_speed(int fd, uint32_t speed)
102
static int texas_load_firmware(int fd, const char *firmware) {
104
int fw = open(firmware, O_RDONLY);
106
fprintf(stdout, "Opening firmware file: %s\n", firmware);
109
"Could not open firmware file %s: %s (%d).\n",
110
firmware, strerror(errno), errno);
112
fprintf(stdout, "Uploading firmware...\n");
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);
119
nr = read(fw, cmdp, sizeof(cmdp));
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");
125
FAILIF(read(fw, data, cmd->plen) != cmd->plen,
126
"Could not read %d bytes of data for command with opcode %04x!\n",
133
fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
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",
148
/* Wait for response */
149
if (read_command_complete(fd,
156
fprintf(stdout, "Firmware upload successful.\n");
162
int texasalt_init(int fd, int speed, struct termios *ti)
164
struct timespec tm = {0, 50000};
166
unsigned char resp[100]; /* Response */
169
memset(resp,'\0', 100);
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 */
175
/* Get Manufacturer and LMP version */
176
cmd[0] = HCI_COMMAND_PKT;
182
n = write(fd, cmd, 4);
184
perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
188
fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
193
if (read_hci_event(fd, resp, 100) < 0) {
194
perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
198
/* Wait for command complete event for our Opcode */
199
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
201
/* Verify manufacturer */
202
if ((resp[11] & 0xFF) != 0x0d)
203
fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
205
/* Print LMP version */
206
fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
208
/* Print LMP subversion */
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] = {
224
fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
227
"\tinternal version freeze: %d\n"
228
"\tsoftware version: %d\n"
231
((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
232
((brf_chip > 7) ? "unknown" : c_brf_chip[brf_chip]),
235
sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
236
texas_load_firmware(fd, fw);
238
texas_change_speed(fd, speed);
240
nanosleep(&tm, NULL);