OSDN Git Service

Code drop from //branches/cupcake/...@124589
[android-x86/external-bluetooth-bluez.git] / utils / tools / hciattach_tialt.c
index 34e27bf..4df91f6 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
 #include <syslog.h>
 #include <termios.h>
 #include <time.h>
+#include <unistd.h>
 #include <sys/time.h>
 #include <sys/poll.h>
 #include <sys/param.h>
@@ -61,16 +61,17 @@ typedef struct {
 } __attribute__((packed)) command_complete_t;
 
 extern int read_hci_event(int fd, unsigned char* buf, int size);
+extern int set_speed(int fd, struct termios *ti, int speed);
 
-static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
+static int read_command_complete(int fd, uint16_t opcode) {
        command_complete_t resp;
        /* Read reply. */
-       FAILIF(read_hci_event(fd, (char *)&resp, sizeof(resp)) < 0,
-                  "Failed to read response");
+       FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
+                  "Failed to read response for opcode %#04x\n", opcode);
        
        /* Parse speed-change reply */
        FAILIF(resp.uart_prefix != HCI_EVENT_PKT,
-                  "Error in response: not an event packet, but 0x%02x!\n", 
+                  "Error in response: not an event packet, but %#02X!\n", 
                   resp.uart_prefix);
 
        FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE, /* event must be event-complete */
@@ -78,14 +79,18 @@ static int read_command_complete(int fd, unsigned short opcode, unsigned char le
                   "but 0x%02x!\n", resp.hci_hdr.evt);
 
        FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
-                  "Error in response: plen is not >= 4, but 0x%02x!\n",
+                  "Error in response: plen is not >= 4, but %#02X!\n",
                   resp.hci_hdr.plen);
 
        /* cmd-complete event: opcode */
        FAILIF(resp.cmd_complete.opcode != (uint16_t)opcode,
-                  "Error in response: opcode is 0x%04x, not 0x%04x!",
+                  "Error in response: opcode is %#04X, not %#04X!",
                   resp.cmd_complete.opcode, opcode);
 
+       FAILIF(resp.status != 0,
+               "Error in response: status is %x for opcode %#04x\n", resp.status,
+                  opcode);
+
        return resp.status == 0 ? 0 : -1;
 }
 
@@ -95,12 +100,9 @@ typedef struct {
        uint32_t speed;
 } __attribute__((packed)) texas_speed_change_cmd_t;
 
-static int texas_change_speed(int fd, uint32_t speed) {
-       
-       return 0;
+static int texas_change_speed(int fd, struct termios *ti, uint32_t speed) {
 
        /* Send a speed-change request */
-
        texas_speed_change_cmd_t cmd;
        int n;
 
@@ -109,18 +111,13 @@ static int texas_change_speed(int fd, uint32_t speed) {
        cmd.hci_hdr.plen = sizeof(uint32_t);
        cmd.speed = speed;
 
-       fprintf(stdout, "Setting speed to %d\n", speed);
+       printf("Setting speed to %d\n", speed);
        n = write(fd, &cmd, sizeof(cmd));
        if (n < 0) {
                perror("Failed to write speed-set command");
                return -1;
        }
 
-       n = write(fd, &cmd, sizeof(cmd));
-       if (n < 0) {
-               perror("Failed to write command.");
-               return -1;
-       }
        if (n < (int)sizeof(cmd)) {
                fprintf(stderr, "Wanted to write %d bytes, could only write %d. "
                                "Stop\n", (int)sizeof(cmd), n);
@@ -128,66 +125,60 @@ static int texas_change_speed(int fd, uint32_t speed) {
        }
 
        /* Parse speed-change reply */
-       if (read_command_complete(fd, 0xff36, 4) < 0) {
+       if (read_command_complete(fd, 0xff36) < 0) {
+               return -1;
+       }
+
+       if (set_speed(fd, ti, speed) < 0) {
+               perror("Can't set baud rate");
                return -1;
        }
-       fprintf(stdout, "Speed changed to %d.\n", speed);
+
+       printf("Texas speed changed to %d.\n", speed);
+       return 0;
 }
 
 static int texas_load_firmware(int fd, const char *firmware) {
+       int fw;
 
-       fprintf(stdout, "Opening firmware file: %s\n", firmware);
-       int fw = open(firmware, O_RDONLY);
-       FAILIF(fw < 0, 
-                  "Could not open firmware file %s: %s (%d).\n",
+       printf("Loading firmware from %s...\n", firmware);
+       fw = open(firmware, O_RDONLY);
+       FAILIF(fw < 0, "Could not open firmware file %s: %s (%d).\n",
                   firmware, strerror(errno), errno);
 
-       fprintf(stdout, "Uploading firmware...\n");
-       do {
-               int nr;
-               /* Read each command and wait for a response. */
-               unsigned char cmdp[1 + sizeof(hci_command_hdr)];
-               nr = read(fw, cmdp, sizeof(cmdp));
-               if (!nr)
-                       break;
-               hci_command_hdr *cmd = (hci_command_hdr *)(cmdp + 1);
-               FAILIF(nr != sizeof(cmdp), "Could not read H4 + HCI header!\n");
-               FAILIF(*cmdp != HCI_COMMAND_PKT, "Command is not an H4 command packet!\n");
-               
-               unsigned char data[1024];
-               FAILIF(read(fw, data, cmd->plen) != cmd->plen,
-                          "Could not read %d bytes of data for command with opcode %04x!\n",
-                          cmd->plen,
-                          cmd->opcode);
-                               
-               {
-#if 0
-                       fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n", 
-                                       cmd->opcode, 
-                                       cmd->plen);
-#endif
-                       struct iovec iov_cmd[2];
-                       iov_cmd[0].iov_base = cmdp;
-                       iov_cmd[0].iov_len      = sizeof(cmdp);
-                       iov_cmd[1].iov_base = data;
-                       iov_cmd[1].iov_len      = cmd->plen;
-                       int nw = writev(fd, iov_cmd, 2);
-                       FAILIF(nw != sizeof(cmd) +      cmd->plen, 
-                                  "Could not send entire command (sent only %d bytes)!\n",
-                                  nw);
-               }
-
-               /* Wait for response */
-               if (read_command_complete(fd, 
-                                                                 cmd->opcode,
-                                                                 cmd->plen) < 0) {
+       while (1) {
+               unsigned char cmd[1024];
+               hci_command_hdr *hci_cmd;
+               unsigned int n;
+
+               /* read in H4 packets from firmware file */
+               n = read(fw, cmd, sizeof(hci_command_hdr) + 1);
+               if (!n) break;
+               FAILIF(n != sizeof(hci_command_hdr) + 1,
+                          "Could not read H4 + HCI header!\n");
+               FAILIF(cmd[0] != HCI_COMMAND_PKT,
+                          "Command is not an H4 command packet!\n");
+               hci_cmd = (hci_command_hdr *)&cmd[1];
+
+               FAILIF(read(fw, &cmd[sizeof(hci_command_hdr) + 1], hci_cmd->plen)
+                          != hci_cmd->plen,
+                          "Could not read %d bytes of data for opcode %#04X!\n",
+                          hci_cmd->plen, hci_cmd->opcode);
+
+               /* write h4 packet to chip */
+//             printf("opcode %#04X (%d bytes data)\n", hci_cmd->opcode, hci_cmd->plen);
+               n = write(fd, cmd, sizeof(hci_command_hdr) + 1 + hci_cmd->plen);
+               FAILIF(n != sizeof(hci_command_hdr) + 1 + hci_cmd->plen,
+                          "Could not send entire command (sent only %d bytes)!\n",
+                          n);
+               if (read_command_complete(fd, hci_cmd->opcode) < 0) {
                        return -1;
                }
-                       
-       } while(1);
-       fprintf(stdout, "Firmware upload successful.\n");
-
+       }
        close(fw);
+
+       printf("Firmware load successful.\n");
+
        return 0;
 }
 
@@ -195,12 +186,12 @@ int texasalt_init(int fd, int speed, struct termios *ti)
 {
        struct timespec tm = {0, 50000};
        char cmd[4];
-       unsigned char resp[100];                /* Response */
+       unsigned char resp[100];  /* Response */
        int n;
 
        memset(resp,'\0', 100);
 
-       /* It is possible to get software version with manufacturer specific 
+       /* It is possible to get software version with manufacturer specific
           HCI command HCI_VS_TI_Version_Number. But the only thing you get more
           is if this is point-to-point or point-to-multipoint module */
 
@@ -235,12 +226,12 @@ int texasalt_init(int fd, int speed, struct termios *ti)
                fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
 
        /* Print LMP version */
-       fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
+       fprintf(stderr, "Texas module LMP version : %#02X\n", resp[10] & 0xFF);
 
        /* Print LMP subversion */
        {
-               unsigned short lmp_subv = resp[13] | (resp[14] << 8);
-               unsigned short brf_chip = (lmp_subv & 0x7c00) >> 10;
+               uint16_t lmp_subv = resp[13] | (resp[14] << 8);
+               uint16_t brf_chip = (lmp_subv & 0x7c00) >> 10;
                static const char *c_brf_chip[5] = {
                        "unknown",
                        "unknown",
@@ -249,7 +240,7 @@ int texasalt_init(int fd, int speed, struct termios *ti)
                        "brf6300"
                };
 
-               fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
+               fprintf(stderr, "Texas module LMP sub-version : %#04X\n", lmp_subv);
 
                fprintf(stderr,
                                "\tinternal version freeze: %d\n"
@@ -262,9 +253,10 @@ int texasalt_init(int fd, int speed, struct termios *ti)
 
                char fw[100];
                sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
-               texas_load_firmware(fd, fw);
 
-               texas_change_speed(fd, speed);
+               texas_change_speed(fd, ti, speed);
+
+               texas_load_firmware(fd, fw);
        }
        nanosleep(&tm, NULL);
        return 0;