Git init
[framework/connectivity/bluez.git] / tools / hciattach_tialt.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2005-2010  Marcel Holtmann <marcel@holtmann.org>
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 #include <sys/socket.h>
43 #include <sys/uio.h>
44
45 #include <bluetooth/bluetooth.h>
46 #include <bluetooth/hci.h>
47 #include <bluetooth/hci_lib.h>
48
49 #include "hciattach.h"
50
51 #define FAILIF(x, args...) do {   \
52         if (x) {                                          \
53                 fprintf(stderr, ##args);  \
54                 return -1;                                \
55         }                                                         \
56 } while(0)
57
58 typedef struct {
59         uint8_t uart_prefix;
60         hci_event_hdr hci_hdr;
61         evt_cmd_complete cmd_complete;
62         uint8_t status;
63         uint8_t data[16];
64 } __attribute__((packed)) command_complete_t;
65
66 static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
67         command_complete_t resp;
68         /* Read reply. */
69         FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
70                    "Failed to read response");
71
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",
75                    resp.uart_prefix);
76
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);
80
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",
83                    resp.hci_hdr.plen);
84
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);
89
90         return resp.status == 0 ? 0 : -1;
91 }
92
93 typedef struct {
94         uint8_t uart_prefix;
95         hci_command_hdr hci_hdr;
96         uint32_t speed;
97 } __attribute__((packed)) texas_speed_change_cmd_t;
98
99 static int texas_change_speed(int fd, uint32_t speed)
100 {
101         return 0;
102 }
103
104 static int texas_load_firmware(int fd, const char *firmware) {
105
106         int fw = open(firmware, O_RDONLY);
107
108         fprintf(stdout, "Opening firmware file: %s\n", firmware);
109
110         FAILIF(fw < 0,
111                    "Could not open firmware file %s: %s (%d).\n",
112                    firmware, strerror(errno), errno);
113
114         fprintf(stdout, "Uploading firmware...\n");
115         do {
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);
120                 int nr;
121                 nr = read(fw, cmdp, sizeof(cmdp));
122                 if (!nr)
123                         break;
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");
126
127                 FAILIF(read(fw, data, cmd->plen) != cmd->plen,
128                            "Could not read %d bytes of data for command with opcode %04x!\n",
129                            cmd->plen,
130                            cmd->opcode);
131
132                 {
133                         int nw;
134 #if 0
135                         fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
136                                         cmd->opcode,
137                                         cmd->plen);
138 #endif
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",
147                                    nw);
148                 }
149
150                 /* Wait for response */
151                 if (read_command_complete(fd,
152                                                                   cmd->opcode,
153                                                                   cmd->plen) < 0) {
154                         return -1;
155                 }
156
157         } while(1);
158         fprintf(stdout, "Firmware upload successful.\n");
159
160         close(fw);
161         return 0;
162 }
163
164 int texasalt_init(int fd, int speed, struct termios *ti)
165 {
166         struct timespec tm = {0, 50000};
167         char cmd[4];
168         unsigned char resp[100];                /* Response */
169         int n;
170
171         memset(resp,'\0', 100);
172
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 */
176
177         /* Get Manufacturer and LMP version */
178         cmd[0] = HCI_COMMAND_PKT;
179         cmd[1] = 0x01;
180         cmd[2] = 0x10;
181         cmd[3] = 0x00;
182
183         do {
184                 n = write(fd, cmd, 4);
185                 if (n < 0) {
186                         perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
187                         return -1;
188                 }
189                 if (n < 4) {
190                         fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
191                         return -1;
192                 }
193
194                 /* Read reply. */
195                 if (read_hci_event(fd, resp, 100) < 0) {
196                         perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
197                         return -1;
198                 }
199
200                 /* Wait for command complete event for our Opcode */
201         } while (resp[4] != cmd[1] && resp[5] != cmd[2]);
202
203         /* Verify manufacturer */
204         if ((resp[11] & 0xFF) != 0x0d)
205                 fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
206
207         /* Print LMP version */
208         fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
209
210         /* Print LMP subversion */
211         {
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] = {
215                         "unknown",
216                         "unknown",
217                         "brf6100",
218                         "brf6150",
219                         "brf6300",
220                         "brf6350",
221                         "unknown",
222                         "wl1271"
223                 };
224                 char fw[100];
225
226                 fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
227
228                 fprintf(stderr,
229                                 "\tinternal version freeze: %d\n"
230                                 "\tsoftware version: %d\n"
231                                 "\tchip: %s (%d)\n",
232                                 lmp_subv & 0x7f,
233                                 ((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
234                                 ((brf_chip > 7) ? "unknown" : c_brf_chip[brf_chip]),
235                                 brf_chip);
236
237                 sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
238                 texas_load_firmware(fd, fw);
239
240                 texas_change_speed(fd, speed);
241         }
242         nanosleep(&tm, NULL);
243         return 0;
244 }