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>
42 #include <sys/socket.h>
45 #include <bluetooth/bluetooth.h>
46 #include <bluetooth/hci.h>
47 #include <bluetooth/hci_lib.h>
49 #include "hciattach.h"
51 #define FAILIF(x, args...) do { \
53 fprintf(stderr, ##args); \
60 hci_event_hdr hci_hdr;
61 evt_cmd_complete cmd_complete;
64 } __attribute__((packed)) command_complete_t;
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)
104 static int texas_load_firmware(int fd, const char *firmware) {
106 int fw = open(firmware, O_RDONLY);
108 fprintf(stdout, "Opening firmware file: %s\n", firmware);
111 "Could not open firmware file %s: %s (%d).\n",
112 firmware, strerror(errno), errno);
114 fprintf(stdout, "Uploading firmware...\n");
116 /* Read each command and wait for a response. */
117 unsigned char data[1024];
118 unsigned char cmdp[1 + sizeof(hci_command_hdr)];
119 hci_command_hdr *cmd = (hci_command_hdr *)(cmdp + 1);
121 nr = read(fw, cmdp, sizeof(cmdp));
124 FAILIF(nr != sizeof(cmdp), "Could not read H4 + HCI header!\n");
125 FAILIF(*cmdp != HCI_COMMAND_PKT, "Command is not an H4 command packet!\n");
127 FAILIF(read(fw, data, cmd->plen) != cmd->plen,
128 "Could not read %d bytes of data for command with opcode %04x!\n",
135 fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
139 struct iovec iov_cmd[2];
140 iov_cmd[0].iov_base = cmdp;
141 iov_cmd[0].iov_len = sizeof(cmdp);
142 iov_cmd[1].iov_base = data;
143 iov_cmd[1].iov_len = cmd->plen;
144 nw = writev(fd, iov_cmd, 2);
145 FAILIF(nw != (int) sizeof(cmd) + cmd->plen,
146 "Could not send entire command (sent only %d bytes)!\n",
150 /* Wait for response */
151 if (read_command_complete(fd,
158 fprintf(stdout, "Firmware upload successful.\n");
164 int texasalt_init(int fd, int speed, struct termios *ti)
166 struct timespec tm = {0, 50000};
168 unsigned char resp[100]; /* Response */
171 memset(resp,'\0', 100);
173 /* It is possible to get software version with manufacturer specific
174 HCI command HCI_VS_TI_Version_Number. But the only thing you get more
175 is if this is point-to-point or point-to-multipoint module */
177 /* Get Manufacturer and LMP version */
178 cmd[0] = HCI_COMMAND_PKT;
184 n = write(fd, cmd, 4);
186 perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
190 fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
195 if (read_hci_event(fd, resp, 100) < 0) {
196 perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
200 /* Wait for command complete event for our Opcode */
201 } while (resp[4] != cmd[1] && resp[5] != cmd[2]);
203 /* Verify manufacturer */
204 if ((resp[11] & 0xFF) != 0x0d)
205 fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
207 /* Print LMP version */
208 fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
210 /* Print LMP subversion */
212 unsigned short lmp_subv = resp[13] | (resp[14] << 8);
213 unsigned short brf_chip = (lmp_subv & 0x7c00) >> 10;
214 static const char *c_brf_chip[8] = {
226 fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
229 "\tinternal version freeze: %d\n"
230 "\tsoftware version: %d\n"
233 ((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
234 ((brf_chip > 7) ? "unknown" : c_brf_chip[brf_chip]),
237 sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
238 texas_load_firmware(fd, fw);
240 texas_change_speed(fd, speed);
242 nanosleep(&tm, NULL);