OSDN Git Service

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