hciattach_tialt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2005-2010 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <syslog.h>
  22. #include <termios.h>
  23. #include <time.h>
  24. #include <poll.h>
  25. #include <sys/time.h>
  26. #include <sys/param.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/uio.h>
  29. #include "lib/bluetooth.h"
  30. #include "lib/hci.h"
  31. #include "lib/hci_lib.h"
  32. #include "hciattach.h"
  33. #define FAILIF(x, args...) do { \
  34. if (x) { \
  35. fprintf(stderr, ##args); \
  36. return -1; \
  37. } \
  38. } while(0)
  39. typedef struct {
  40. uint8_t uart_prefix;
  41. hci_event_hdr hci_hdr;
  42. evt_cmd_complete cmd_complete;
  43. uint8_t status;
  44. uint8_t data[16];
  45. } __attribute__((packed)) command_complete_t;
  46. static int read_command_complete(int fd, unsigned short opcode, unsigned char len) {
  47. command_complete_t resp;
  48. /* Read reply. */
  49. FAILIF(read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0,
  50. "Failed to read response");
  51. /* Parse speed-change reply */
  52. FAILIF(resp.uart_prefix != HCI_EVENT_PKT,
  53. "Error in response: not an event packet, but 0x%02x!\n",
  54. resp.uart_prefix);
  55. FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE, /* event must be event-complete */
  56. "Error in response: not a cmd-complete event, "
  57. "but 0x%02x!\n", resp.hci_hdr.evt);
  58. FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
  59. "Error in response: plen is not >= 4, but 0x%02x!\n",
  60. resp.hci_hdr.plen);
  61. /* cmd-complete event: opcode */
  62. FAILIF(resp.cmd_complete.opcode != (uint16_t)opcode,
  63. "Error in response: opcode is 0x%04x, not 0x%04x!",
  64. resp.cmd_complete.opcode, opcode);
  65. return resp.status == 0 ? 0 : -1;
  66. }
  67. typedef struct {
  68. uint8_t uart_prefix;
  69. hci_command_hdr hci_hdr;
  70. uint32_t speed;
  71. } __attribute__((packed)) texas_speed_change_cmd_t;
  72. static int texas_change_speed(int fd, uint32_t speed)
  73. {
  74. return 0;
  75. }
  76. static int texas_load_firmware(int fd, const char *firmware) {
  77. int fw = open(firmware, O_RDONLY);
  78. fprintf(stdout, "Opening firmware file: %s\n", firmware);
  79. FAILIF(fw < 0,
  80. "Could not open firmware file %s: %s (%d).\n",
  81. firmware, strerror(errno), errno);
  82. fprintf(stdout, "Uploading firmware...\n");
  83. do {
  84. /* Read each command and wait for a response. */
  85. unsigned char data[1024];
  86. unsigned char cmdp[1 + sizeof(hci_command_hdr)];
  87. hci_command_hdr *cmd = (hci_command_hdr *)(cmdp + 1);
  88. int nr;
  89. nr = read(fw, cmdp, sizeof(cmdp));
  90. if (!nr)
  91. break;
  92. FAILIF(nr != sizeof(cmdp), "Could not read H4 + HCI header!\n");
  93. FAILIF(*cmdp != HCI_COMMAND_PKT, "Command is not an H4 command packet!\n");
  94. FAILIF(read(fw, data, cmd->plen) != cmd->plen,
  95. "Could not read %d bytes of data for command with opcode %04x!\n",
  96. cmd->plen,
  97. cmd->opcode);
  98. {
  99. int nw;
  100. #if 0
  101. fprintf(stdout, "\topcode 0x%04x (%d bytes of data).\n",
  102. cmd->opcode,
  103. cmd->plen);
  104. #endif
  105. struct iovec iov_cmd[2];
  106. iov_cmd[0].iov_base = cmdp;
  107. iov_cmd[0].iov_len = sizeof(cmdp);
  108. iov_cmd[1].iov_base = data;
  109. iov_cmd[1].iov_len = cmd->plen;
  110. nw = writev(fd, iov_cmd, 2);
  111. FAILIF(nw != (int) sizeof(cmd) + cmd->plen,
  112. "Could not send entire command (sent only %d bytes)!\n",
  113. nw);
  114. }
  115. /* Wait for response */
  116. if (read_command_complete(fd,
  117. cmd->opcode,
  118. cmd->plen) < 0) {
  119. return -1;
  120. }
  121. } while(1);
  122. fprintf(stdout, "Firmware upload successful.\n");
  123. close(fw);
  124. return 0;
  125. }
  126. int texasalt_init(int fd, int speed, struct termios *ti)
  127. {
  128. struct timespec tm = {0, 50000};
  129. char cmd[4];
  130. unsigned char resp[100]; /* Response */
  131. int n;
  132. memset(resp,'\0', 100);
  133. /* It is possible to get software version with manufacturer specific
  134. HCI command HCI_VS_TI_Version_Number. But the only thing you get more
  135. is if this is point-to-point or point-to-multipoint module */
  136. /* Get Manufacturer and LMP version */
  137. cmd[0] = HCI_COMMAND_PKT;
  138. cmd[1] = 0x01;
  139. cmd[2] = 0x10;
  140. cmd[3] = 0x00;
  141. do {
  142. n = write(fd, cmd, 4);
  143. if (n < 0) {
  144. perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
  145. return -1;
  146. }
  147. if (n < 4) {
  148. fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
  149. return -1;
  150. }
  151. /* Read reply. */
  152. if (read_hci_event(fd, resp, 100) < 0) {
  153. perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
  154. return -1;
  155. }
  156. /* Wait for command complete event for our Opcode */
  157. } while (resp[4] != cmd[1] && resp[5] != cmd[2]);
  158. /* Verify manufacturer */
  159. if ((resp[11] & 0xFF) != 0x0d)
  160. fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
  161. /* Print LMP version */
  162. fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
  163. /* Print LMP subversion */
  164. {
  165. unsigned short lmp_subv = resp[13] | (resp[14] << 8);
  166. unsigned short brf_chip = (lmp_subv & 0x7c00) >> 10;
  167. static const char *c_brf_chip[8] = {
  168. "unknown",
  169. "unknown",
  170. "brf6100",
  171. "brf6150",
  172. "brf6300",
  173. "brf6350",
  174. "unknown",
  175. "wl1271"
  176. };
  177. char fw[100];
  178. fprintf(stderr, "Texas module LMP sub-version : 0x%04x\n", lmp_subv);
  179. fprintf(stderr,
  180. "\tinternal version freeze: %d\n"
  181. "\tsoftware version: %d\n"
  182. "\tchip: %s (%d)\n",
  183. lmp_subv & 0x7f,
  184. ((lmp_subv & 0x8000) >> (15-3)) | ((lmp_subv & 0x380) >> 7),
  185. ((brf_chip > 7) ? "unknown" : c_brf_chip[brf_chip]),
  186. brf_chip);
  187. sprintf(fw, "/etc/firmware/%s.bin", c_brf_chip[brf_chip]);
  188. texas_load_firmware(fd, fw);
  189. texas_change_speed(fd, speed);
  190. }
  191. nanosleep(&tm, NULL);
  192. return 0;
  193. }